【问题标题】:ajax POST in .NET MVC 4 parameters are not resolved.NET MVC 4 参数中的 ajax POST 未解析
【发布时间】:2014-09-19 00:27:18
【问题描述】:

我认为有这个功能:

    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);
}

现在,我在 ajax 调用之前的视图中放置了一个断点,并在操作顶部的控制器中放置了另一个断点。根据 ajax 调用之前的检查,我在视图中的 groupItem 对象中设置的属性都按预期填充。但是,在动作的断点处,GroupItem 参数中的所有属性都是默认值。

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

【问题讨论】:

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

标签: ajax asp.net-mvc


【解决方案1】:

您需要先使用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');
});

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      相关资源
      最近更新 更多