阿贾克斯后到ASP.net MVC控制器 - 对象的属性为null [英] Ajax post to ASP.net MVC controller - object properties are null
问题描述
我有一个ajax后像这样正在建设中:
VAR的myData = [
{
ID:一个,
名称:名称1
},
{
ID:b的,
名称:名称2
}
]。
$阿贾克斯({
键入:POST,
网址:'/ myurl / MyAction的,
数据:{项目:myData的},
数据类型:JSON,
错误:函数(ERR){
警报(错误 - + ERR);
}
});
和一个MVC控制器:
[HttpPost]
公共JsonResult MyAction的(MyClass的[]项)
{
}
MyClass的
的数据只是一个简单的重新presentation:
公共类MyClass的{
公共字符串名称{;组; }
公共字符串ID {获得;组; }
}
当的JavaScript,使POST请求,控制器动作的确收到2项,但是属性(ID,姓名),这些项目都为空。
检查提琴手的要求,身体看起来是这样的:
名称|值
项目[0] [名] |名称1
项目[0] [ID] |一个
项目[1] [名] |名称2
项目[1] [ID] | b
有我错过了什么?
有我错过了什么?
是的,采取下面的文章一起来看看要明白,默认的模型绑定预计,结合收集正确的有线格式。换句话说,为此工作,而不是,
项目[0] [名] |名称1
项目[0] [ID] |一个
项目[1] [名] |名称2
项目[1] [ID] | b
你的有效载荷应该是这个样子的:
项目[0] .Name点|名称1
项目[0] .ID |一个
项目[1] .Name点|名称2
项目[1] .ID | b
不幸的是与jQuery它可以是相当令人沮丧实现这一目标的有效载荷。为此,我建议你使用JSON的有效载荷,如果你想复杂的对象/数组发送到您的服务器使用AJAX:
$。阿贾克斯({
键入:POST,
网址:'/ myurl / MyAction的,
数据:JSON.stringify({项目:myData的}),
的contentType:应用/ JSON的,
错误:函数(ERR){
警报(错误 - + ERR);
}
});
旅游注意:
-
数据:JSON.stringify({项目:myData的})
而不是数据:{项目:myData的}
- 新增
的contentType:应用/ JSON的
- 在摆脱了
的数据类型:'json的
现在你的有效载荷是这样的:
{项目:[{ID:一,名:名称1},{ID:B,名: 姓名2}]}
I've got an ajax post being constructed like this:
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:
[HttpPost]
public JsonResult MyAction(MyClass[] items)
{
}
MyClass
is just a simple representation of the data:
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.
Checking the request in fiddler, the body looks like this:
Name | Value
items[0][Name] | Name 1
items[0][Id] | a
items[1][Name] | Name 2
items[1][Id] | b
Have I missed something?
Have I missed something?
Yes, take a look at the following article to 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:
$.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 ofdata: { items: myData }
- Added
contentType: 'application/json'
- Gotten rid of
dataType: 'json'
Now your payload looks like this:
{"items":[{"id":"a","name":"Name 1"},{"id":"b","name":"Name 2"}]}
这篇关于阿贾克斯后到ASP.net MVC控制器 - 对象的属性为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!