1

您好,我正在尝试让 ASP.NET MVC 将数据中包含 json 的请求解析为模型,但由于某种原因,它无法正常工作。

这是我的要求

        $.ajax({
            type: "POST",
            url: "/ajax/album/SaveAlbumImages/",
            data: { model: ko.toJSON(model) },
            dataType: "json",
            success: function (data: BaseJson<string>, textStatus, jqXHR) {

            }
        });

在此处输入图像描述

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:2685
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:ASP.NET_SessionId=gtjnl1hr2st3szm50mwmlmb4; __RequestVerificationToken=AWXbN4FYcsQX4Wt-bWXTtCWmPAtBf_5ShztUtZnh6DQ552xLiZoUbsAtCsQJXYly7zF3F6nJNYaKJbu5QTucMrD1sv_vX2qj9Y0Db4OOksg1; .ASPXAUTH=677B1CD5D4AE1BF1B500EED6D9E465B79F7068187B1018671FCAA38E518DE12E93B9C082E27CB39C833DFC53FFBC78099D97F72E6E388A3895C1961FB75E5FDC0DCCE4210201D85866C7F59384135D2B; Cosplay.locale=da-DK
Host:localhost:52972
Origin:http://localhost:52972
Referer:http://localhost:52972/album/managealbumimagesnew/1221
User-Agent:Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
model:{"AlbumId":1221,"Images":[{"ImageId":2454,"Description":null},{"ImageId":2455,"Description":null},{"ImageId":2456,"Description":null},{"ImageId":2457,"Description":null},{"ImageId":2458,"Description":null},{"ImageId":2459,"Description":null},{"ImageId":2460,"Description":null},{"ImageId":2461,"Description":null},{"ImageId":2462,"Description":null},{"ImageId":2463,"Description":null},{"ImageId":2464,"Description":null},{"ImageId":2465,"Description":null},{"ImageId":2466,"Description":null},{"ImageId":2467,"Description":null},{"ImageId":2468,"Description":null},{"ImageId":2469,"Description":null},{"ImageId":2470,"Description":null},{"ImageId":2471,"Description":null},{"ImageId":2472,"Description":null},{"ImageId":2473,"Description":null},{"ImageId":2474,"Description":null},{"ImageId":2475,"Description":null},{"ImageId":2476,"Description":null},{"ImageId":2477,"Description":null},{"ImageId":2478,"Description":null},{"ImageId":2479,"Description":null},{"ImageId":2480,"Description":null},{"ImageId":2481,"Description":null},{"ImageId":2482,"Description":null},{"ImageId":2483,"Description":null},{"ImageId":2484,"Description":null},{"ImageId":2485,"Description":null},{"ImageId":2486,"Description":null},{"ImageId":2487,"Description":null},{"ImageId":2488,"Description":null},{"ImageId":2489,"Description":null},{"ImageId":2490,"Description":null},{"ImageId":2491,"Description":null},{"ImageId":2492,"Description":null},{"ImageId":2493,"Description":null},{"ImageId":2494,"Description":null},{"ImageId":2495,"Description":null},{"ImageId":2496,"Description":null},{"ImageId":2497,"Description":null},{"ImageId":2498,"Description":null},{"ImageId":2499,"Description":null},{"ImageId":2500,"Description":null}]}

这是我在 MVC 中的代码

public ActionResult SaveAlbumImages(AlbumImagesSaveModel model)
        {

和模型本身

public class AlbumImagesSaveModel
{
    public int AlbumId { get; set; }
    public List<AlbumImageSaveModel> Images { get; set; } 
}
public class AlbumImageSaveModel
{
    public int ImageId { get; set; }
    public string Description { get; set; }
}

我究竟做错了什么?

4

1 回答 1

5

您正在发送 JSON,因此请在Content-Type请求标头中指定,否则服务器无法知道您的意图:

$.ajax({
    type: 'POST',
    url: '/ajax/album/SaveAlbumImages/',
    contentType: 'application/json',
    data: ko.toJSON(model),
    success: function (data, textStatus, jqXHR) {

    }
});

还要注意我们是如何包装整个data参数来发送 JSON 的,而无需解耦它。我还删除了dataType: 'json'参数,就好像您的服务器发送了适当的 Content-Type 响应标头(如果您从您的操作返回 JsonResult ,这就是 ASP.NET MVC 所做的),jQuery 能够自动推断响应类型并提供正确的类型到您的成功回调。

此外,您的回调有一些非常糟糕的 JavaScript 语法。您似乎试图为成功函数的数据参数提供某种类型,这是无效的 javascript。

于 2013-07-13T14:41:24.987 回答