ASP.NET MVC模型与jQuery ajax请求的绑定 [英] ASP.NET MVC Model Binding with jQuery ajax request
问题描述
我的MVC代码中有一个服务器端对象,如下所示:
I have a server side object in my MVC code that looks like this:
class Person
{
public long Id { get; set; }
public string Name { get; set; }
public IList<Friend> Friends { get; set; }
}
class Friend
{
public long Id { get; set; }
public string Name { get; set; }
}
我正在从jQuery客户端发出ajax请求并发送以下数据(为提高可读性而引入了换行符):
From the jQuery client, I am making an ajax request and sending the following data (line breaks introduced for readability):
var personId = $(...
var personName = $(...
var friends = []; // array of associative arrays
for (var i = 0; i < _friends.length; i++) // _friends is is a global array object and is different from friends below
{
var friendId = $(...
var friendName = $(...)
friends[i] = { 'Id' : friendId, 'Name' : friendName };
}
...以此类推,将值转换为变量
... and so on to get values into variables
请注意,我发送的内容应作为JavaScript数组发送到 Person.Friends
,即 IList< T>
,其中该数组的每个元素都是一个关联列表具有与服务器端 Friend
类类似的属性.
Please observe that I am sending what should go into Person.Friends
, the IList<T>
as a JavaScript array where each element of the array is an associative list that has properties just like the server side Friend
class.
这是我的ajax请求:
Here's my ajax request:
$.ajax('/Strings/JsonCreateNew',
{
cache: false, async: false, type: 'POST',
data: {
'Id' : personId,
'Name' : personName,
'Friends' : friends},
dataType: 'json',
error: SaveNewFriendClientSideHandler.OnError,
success: SaveNewFriendClientSideHandler.OnSuccess
});
在我的控制器中,只要我有一个 FormCollection
来接收数据,就可以获取所有数据,但是我想绑定到强类型的Person对象,所以如果我更改我的控制器定义来自:
In my controller, as long as I have a FormCollection
to receive the data, I get all my data, but I'd like to bind to the strongly typed Person object, so if I change my controller definition from:
[HttpPost]
public ActionResult JsonCreateNew(FromCollection person)
{
// I get all the data in the person.AllKeys property
return new EmptyResult();
}
收件人:
[HttpPost]
public ActionResult JsonCreateNew(Person person)
{
// the default ASP.NET MVC model binder isn't able to bind to this
// in fact, the routing module does not even route the request to this
// action, so this action never gets fired.
return new EmptyResult();
}
我仍然可以获取所有数据,但是我仍然真的很担心 IList< Friend>
.
I still do get all my data but I am really still concerned about the IList<Friend>
.
它在 Person
对象中作为一个数组接收,该数组具有正确数量的 Friends
对象,并带有正确的属性名称( Name
和 Id
),但每个 Friend
对象的两个属性的值均为 null
或0,即,即使有值由发送,也不会初始化客户.
It is received in the Person
object as an array that has the correct number of Friends
objects with the right property names (Name
and Id
) but the values of both the properties of each Friend
object are null
or 0, i.e. not initialized even when there are values being sent by the client.
请帮助.
推荐答案
MVC中存在一个错误,他们拒绝修复以下问题:如果集合属性名称以类型名称开头,则该属性不会绑定.更改
There is a bug in MVC they refuse to fix that if the colection property name begins with the type name it does not bind. Change
public IList<Friend> Friends { get; set; }
收件人:
public IList<Friend> Biscuits { get; set; }
只是暂时的.会起作用的.
just temporary. It will work.
这篇关于ASP.NET MVC模型与jQuery ajax请求的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!