C# Ajax 发布到 ASP.net MVC 控制器 - 对象属性为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16547491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:18:39  来源:igfitidea点击:

Ajax post to ASP.net MVC controller - object properties are null

c#javascriptajaxasp.net-mvc

提问by Alex

I've got an ajax post being constructed like this:

我有一个这样构造的ajax帖子:

var myData = [
    {
        id: "a",
        name: "Name 1"
    },
    {
        id: "b",
        name: "Name 2"
    }
];

$.ajax({
    type: 'POST',
    url: '/myurl/myAction',
    data: { items: myData },
    dataType: 'json',
    error: function (err) {
        alert("error - " + err);
    }
});

And an MVC controller:

还有一个 MVC 控制器:

[HttpPost]
public JsonResult MyAction(MyClass[] items)
{

}

MyClassis just a simple representation of the data:

MyClass只是数据的简单表示:

public class MyClass {
    public string Name {get; set; }
    public string Id {get; set; }
}

When the javascript makes the post request, the controller action does indeed receive 2 items, however the properties (id, name) in these items are null.

当 javascript 发出 post 请求时,控制器操作确实收到 2 个项目,但是这些项目中的属性(id、name)为空。

Checking the request in fiddler, the body looks like this:

在 fiddler 中检查请求,正文如下所示:

Name                 | Value
items[0][Name]       | Name 1
items[0][Id]         | a
items[1][Name]       | Name 2
items[1][Id]         | b

Have I missed something?

我错过了什么吗?

采纳答案by Darin Dimitrov

Have I missed something?

我错过了什么吗?

Yes, take a look at the following articleto understand the correct wire format that the default model binder expects for binding collections. In other words, for this to work, instead of:

是的,请查看以下文章以了解默认模型绑定器期望用于绑定集合的正确连线格式。换句话说,要使其正常工作,而不是:

items[0][Name]       | Name 1
items[0][Id]         | a
items[1][Name]       | Name 2
items[1][Id]         | b

your payload should have looked like this:

你的有效载荷应该是这样的:

items[0].Name       | Name 1
items[0].Id         | a
items[1].Name       | Name 2
items[1].Id         | b

Unfortunately with jQuery it can be quite frustrating to achieve this payload. For this reason I would recommend that you use a JSON payload if you want to send complex objects/arrays to your server with AJAX:

不幸的是,使用 jQuery 实现此有效负载可能会非常令人沮丧。出于这个原因,如果您想使用 AJAX 将复杂对象/数组发送到您的服务器,我建议您使用 JSON 负载:

$.ajax({
    type: 'POST',
    url: '/myurl/myAction',
    data: JSON.stringify({ items: myData }),
    contentType: 'application/json',
    error: function (err) {
        alert("error - " + err);
    }
});

Things to notice:

注意事项:

  • data: JSON.stringify({ items: myData })instead of data: { items: myData }
  • Added contentType: 'application/json'
  • Gotten rid of dataType: 'json'
  • data: JSON.stringify({ items: myData })代替 data: { items: myData }
  • 添加 contentType: 'application/json'
  • 摆脱了 dataType: 'json'

Now your payload looks like this:

现在你的有效载荷看起来像这样:

{"items":[{"id":"a","name":"Name 1"},{"id":"b","name":"Name 2"}]}

回答by HamidReza

you can use this code to solve the problem :

您可以使用此代码来解决问题:

$.ajax({
    url: '/myurl/myAction',
    data: { '': items },
    method: "POST",
    dataType: 'json',
    success: function (xhr, status, response) {
    },
    error: function (xhr, status, response) {
    }
});

[HttpPost]
public JsonResult MyAction(IEnumerable<MyClass> items)
{

}