1

我通过一个 jQuery AJAX 调用将 Json 传回给一个 MVC 函数,该函数需要一个Folder. MVC 正确解析了一些数据,但不是我发回的列表。

MVC

public class Folder : IValidate
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<SearchCriteria> SearchCriteria { get; set; }
}

public class SearchCriteria
{
    public int FolderId { get; set; }
    public int SettingsEntryID { get; set; }
    public string SearchParameter { get; set; }
}

public ActionResult EditFolder(Folder folder)
{
   service.EditFolder(folder);
   return this.Json(Json(new { Success = true }));
}

Javascript

var folder = {
    Id: $("#groupID").val(),
    Name: $("#groupName").val(),
    SearchCriteria: []
};

$(".searchCriteria").each(function () {
    folder.SearchCriteria.push(
        {
            FolderId: $("#groupID").val(),
            SearchParameter: $(this).val(),
            SettingsEntryID: $(this).attr("id").replace("searchCriteria", "")
        });
});

$.ajax({
    url: "/settings/editfolder/",
    type: "POST",
    dataType: "json",
    data: folder,
    traditional: true,
    success: function (data) {
        alert("wsaved");
    }
});

folder, 在此函数中设置IdName但未SearchCriteria正确设置。它设置为null。如果我注释掉traditional: true列表,则会创建列表,但每个列表的所有值SearchCriteria都是0or null

我错过了什么吗?

4

1 回答 1

2

你错过了两点

1. contentType: 'application/json; charset=utf-8', 

2. data: JSON.stringify(folder)

并一更正

网址应该是

url : "@Url.Action("ActionName", "ControllerName", new { area = "AreaName" })"

jQuery

$.ajax({
    url: "@Url.Action("Action", "Controller", new { area = "Area" })",
    type: "POST",
    contentType: 'application/json; charset=utf-8', 
    dataType: "json",
    data: JSON.stringify(folder),
    traditional: true,
    success: function (data) {
        alert("wsaved");
    }
});
于 2013-08-20T08:55:49.153 回答