jquery - 我的对象没有使用 jQuery 的 ASP.NET MVC 发布
问题描述
我有以下 javascript:
<script>
function uploadPhotoDoc() {
var SmallPhotoDoc = {};
SmallPhotoDoc.PK_PhotoDoc = $("#PK_PhotoDoc").val();
SmallPhotoDoc.FK_Site = $("#FK_Site").val();
SmallPhotoDoc.SeriesID = $("#SeriesID").val();
SmallPhotoDoc.ContentID = $("#ContentID").val();
SmallPhotoDoc.Notes = $("#Notes").val();
SmallPhotoDoc.Date = $("#Date").val();
data = "{SmallPhotoDoc: " + JSON.stringify(SmallPhotoDoc) + "} ";
alert(data);
$.ajax({
type: "POST",
url: "/Containers/UploadPhotoDoc/@ViewContext.RouteData.Values["org"]",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}
</script>
我在 C# 中有以下课程:
public class SmallPhotoDoc
{
public Guid PK_PhotoDoc { get; set; }
public Guid? FK_Site { get; set; }
public string SeriesID { get; set; }
public string ContentID { get; set; }
public string Notes { get; set; }
public DateTime? Date { get; set; }
}
我有以下签名要发送到:
public string UploadPhotoDoc(string id, SmallPhotoDoc model)
我已经通过弹出警报验证了数据是否正确。但是当我进入UploadPhotoDoc
菜单时,模型总是为空。有人看到我的错误吗?请注意,我尝试使用[FromBody]
模型参数,但没有做任何事情。它显示请求对象上没有发布任何表单。
解决方案
在 JS 中:
function uploadPhotoDoc(id) {
var SmallPhotoDoc = {};
SmallPhotoDoc.PK_PhotoDoc = $("#PK_PhotoDoc").val();
SmallPhotoDoc.FK_Site = $("#FK_Site").val();
SmallPhotoDoc.SeriesID = $("#SeriesID").val();
SmallPhotoDoc.ContentID = $("#ContentID").val();
SmallPhotoDoc.Notes = $("#Notes").val();
SmallPhotoDoc.Date = $("#Date").val();
var model = {
"id": @ViewContext.RouteData.Values["org"],
"model": SmallPhotoDoc
}
alert(model);
$.ajax({
type: "POST",
url: "/Containers/UploadPhotoDoc",
data: model,
datatype: "json",
cache: false,
success: function (response) {
alert(response);
},
failure: function (response) {
alert(response.responseText);
},
error: function (response) {
alert(response.responseText);
}
});
}
在控制器的动作方法中:
public string UploadPhotoDoc(string id, SmallPhotoDoc model)
推荐阅读
- java - 如何将两个xml文件之间的差异输出到另一个xml文件中
- python - 在不使用 Numpy 或 zip 的情况下检查给定列表中的山丘或山谷的功能?
- sql-server - 获取 ID 在另一个表中不存在
- android - 在 Android 的 MaterialDatePicker 中禁用取消按钮
- c# - IDX:20803 无法从...获取配置 - Web API
- c# - 文件夹选择器未经授权访问子目录和其中的文件 UWP
- ios - 找不到“FMDB.h”文件
- akka - Akka HTTP 服务器:Acceptor 线程处于阻塞状态
- shell - 从文件中提取字符串部分并存储为带有文件名的列表
- python - 为什么这 i = (a += 2) 在 Python 中给出 SyntaxError?