NET MVC 4中的ajax POST参数未解析

NET MVC 4中的ajax POST参数未解析,ajax,asp.net-mvc,Ajax,Asp.net Mvc,在我看来,我有这个功能: function EditButtonClick(e1) { var urlString = '@Url.Action( "Create", "Group")'; var groupItem = { GroupCode: e1.getAttribute("data-GroupCode"), GroupType: e1.getAttribute("data-GroupType"), Descripti

在我看来,我有这个功能:

    function EditButtonClick(e1) {
    var urlString = '@Url.Action( "Create", "Group")';
    var groupItem = {
        GroupCode: e1.getAttribute("data-GroupCode"),
        GroupType: e1.getAttribute("data-GroupType"),
        Description: e1.getAttribute("data-Description"),
        InternalNotes: e1.getAttribute("data-InternalNotes"),
        StatusCode: e1.getAttribute("data-StatusCode"),
        Edit: "true"
    };

    $.ajax({
        type: 'POST',
        url: urlString,
        data: { '': groupItem },
        dataType: 'json'
    }).fail(function () {
        alert('Edit process failed');
    });

}
我的视图模型如下所示:

    [Serializable]
    public class GroupItem : ApplicationModel, IValidatableObject

{

public GroupItem() { }

[DisplayName("Group Code")]
public int GroupCode { get; set; }

public string GroupTypeDescription { get; set; }
[DisplayName("Group Type")]
public int GroupType { get; set; }

[DisplayName("Group Name")]
public string Description { get; set; }

[DisplayName("Internal Notes")]
public string InternalNotes { get; set; }
public string PartialInternalNotes { get; set; }

public string Status { get; set; }
[DisplayName("Status Code")]

public int StatusCode { get; set; }

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm:ss}")]
public DateTime EnterTime { get; set; }
public string UserId { get; set; }
public string ActionType { get; set; }
public bool Edit { get; set; }
[HttpPost]
public ActionResult Create(GroupItem groupItem)
{
    if (ModelState.IsValid)
    {
        _groupService.SaveGroup(groupItem);
        return RedirectToAction("List", "Group", new { showAll = false });  
    }
    ViewBag.GroupTypeList = _MasterDataService.GetCodeMasterList((int)Constants.CodeType.GroupType);
    ViewBag.StatusList = _MasterDataService.GetCodeMasterList((int)Constants.CodeType.GroupStatus);
    if (groupItem.GroupCode > 0)groupItem.Edit = true;
    return this.RazorView(groupItem);
}
最后,我的行动如下:

    [Serializable]
    public class GroupItem : ApplicationModel, IValidatableObject

{

public GroupItem() { }

[DisplayName("Group Code")]
public int GroupCode { get; set; }

public string GroupTypeDescription { get; set; }
[DisplayName("Group Type")]
public int GroupType { get; set; }

[DisplayName("Group Name")]
public string Description { get; set; }

[DisplayName("Internal Notes")]
public string InternalNotes { get; set; }
public string PartialInternalNotes { get; set; }

public string Status { get; set; }
[DisplayName("Status Code")]

public int StatusCode { get; set; }

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy HH:mm:ss}")]
public DateTime EnterTime { get; set; }
public string UserId { get; set; }
public string ActionType { get; set; }
public bool Edit { get; set; }
[HttpPost]
public ActionResult Create(GroupItem groupItem)
{
    if (ModelState.IsValid)
    {
        _groupService.SaveGroup(groupItem);
        return RedirectToAction("List", "Group", new { showAll = false });  
    }
    ViewBag.GroupTypeList = _MasterDataService.GetCodeMasterList((int)Constants.CodeType.GroupType);
    ViewBag.StatusList = _MasterDataService.GetCodeMasterList((int)Constants.CodeType.GroupStatus);
    if (groupItem.GroupCode > 0)groupItem.Edit = true;
    return this.RazorView(groupItem);
}
现在,我在ajax调用之前的视图中放置了一个断点,在操作顶部的控制器中放置了另一个断点。根据ajax调用之前的检查,我在视图中的groupItem对象中设置的属性都按预期填充。但是,在操作的断点处,GroupItem参数中的所有属性都是默认值

在另一个视图中的其他代码中,我通过表单提交调用这个非常相同的操作方法。在这种情况下,将按预期填充所有属性。我在ajax调用中遗漏了什么?

试试以下方法:

$.ajax({
        type: 'POST',
        url: urlString,
        data: { 'groupItem': groupItem },
        dataType: 'json'
    }).fail(function () {
        alert('Edit process failed');
    });

您需要使用
JSON.stringify
首先将对象序列化为JSON,然后指定
contentType
,以便服务器理解它的JSON数据

因此,完成AJAX函数调用后

$.ajax({
    type: 'POST',
    url: urlString,
    data: JSON.stringify(groupItem),
    contentType: "application/json",
    dataType: 'json'
}).fail(function () {
    alert('Edit process failed');
});

是否将其更改为
data:JSON.stringify(groupItem)、contentType:“application/JSON”、
help?在
data
中使用空字符串作为键可能是问题的根源,因此Dennis的建议应该可以解决it@DennisR-这种组合解决了我的问题,尽管我的做法是(空键,无contentType)正在另一个视图中工作。可能不同的是,我的控制器扩展了controller,而另一个扩展了ApiController。无论如何,如果你想将你的评论设置为答案,我会选择它。谢谢。@KellyCline很高兴现在可以用了。根据你的建议,现在将我的评论作为答案发布。