首页 > 解决方案 > 我的对象没有使用 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]模型参数,但没有做任何事情。它显示请求对象上没有发布任何表单。

标签: jqueryasp.net-mvc

解决方案


在 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)

推荐阅读