通过发布JSON对象数组来ASP.Net MVC3

问题描述:

我正在寻找一个解决方案,通过JSON POST操作对象MVC3的数组。

I'm looking for a solution to POSTing an array of objects to MVC3 via JSON.

举例code我工作过的:
http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-$p$pview-1.aspx

Example code I'm working off of: http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-1.aspx

JS:

var data = { ItemList: [ {Str: 'hi', Enabled: true} ], X: 1, Y: 2 };

$.ajax({
    url: '/list/save',
    data: JSON.stringify(data),
    success: success,
    error: error,
    type: 'POST',
    contentType: 'application/json, charset=utf-8',
    dataType: 'json'
});

ListViewModel.cs:

ListViewModel.cs:

public class ListViewModel
{
    public List<ItemViewModel> ItemList { get; set; }
    public float X { get; set; }
    public float Y { get; set; }
}

ItemViewModel.cs:

ItemViewModel.cs:

public class ItemViewModel
{
    public string Str;   // originally posted with: { get; set; }
    public bool Enabled; // originally posted with: { get; set; }
}

ListController.cs:

ListController.cs:

public ActionResult Save(ListViewModel list)
{
    // Do something
}

这篇文章的结果是:

The result of this POST:

列表设置,在ListViewModel结果
它的X和Y属性设置结果
底层ITEMLIST属性设置结果
该ITEMLIST包含了一个项目,因为它应该结果
在ITEMLIST该项目是初始化。 str为null,并启用是假的。

list is set, to a ListViewModel
Its X and Y properties are set
The underlying ItemList property is set
The ItemList contains one item, as it should
The item in that ItemList is uninitialized. Str is null and Enabled is false.

换句话说,这是我从MVC3模型得到的结合:

Put another way, this is what I get from MVC3's model binding:

list.X == 1
list.Y == 2
list.ItemList != null
list.ItemList.Count == 1
list.ItemList[0] != null
list.ItemList[0].Str == null

这将出现在MVC3 JsonValueProvider不工作了复杂的对象。我如何得到这个工作?我是否需要修改现有的MVC3 JsonValueProvider并修复它?如果是这样,我怎么得到它,并在MVC3项目取代它呢?

It would appear the MVC3 JsonValueProvider is not working for complex objects. How do I get this to work? Do I need to modify the existing MVC3 JsonValueProvider and fix it? If so, how do I get at it and replace it in an MVC3 project?

我已经追求不果相关*的问题:

Related * questions I've already pursued to no avail:

Asp.net阿贾克斯的mvc JSON(post数组)
使用MVC2及以上基于表单的编码 - 这种方法失败,包含对象(JQuery的不带code得当)

Asp.net Mvc Ajax Json (post Array) Uses MVC2 and older form-based encoding - that approach fails with an object that contains an array of objects (JQuery fails to encode it properly).

Post使用JSON,JQuery的复杂物体ASP.NET MVC控制器数组
使用一个黑客,我想,以避免其中控制器,而不是接受它,然后手动反序列化本身,而不是利用框架纯字符串。

Post an array of complex objects with JSON, JQuery to ASP.NET MVC Controller Uses a hack I'd like to avoid where the Controller instead receives a plain string which it then manually deserializes itself, rather than leveraging the framework.

MVC3 RC2 JSON POST绑定工作不正常...
没有他的内容类型集 - 这是在我的code设置

MVC3 RC2 JSON Post Binding not working correctly ... Didn't have his content-type set - it's set in my code.

How发布使用JSON,JQuery的复杂对象的数组ASP.NET MVC控制器
这个可怜的家伙不得不写一个JsonFilter只是解析数组。另劈我preFER避免的。

How to post an array of complex objects with JSON, JQuery to ASP.NET MVC Controller This poor guy had to write a JsonFilter just to parse an array. Another hack I'd prefer to avoid.

所以,我怎么做到这一点?

So, how do I make this happen?

的问题是,在那名在列表中的模型中的属性没有得到/她们在公共属性设置的。换句话说,MVC3的自动JSON绑定只适用于已获得并设置对象的属性。

The problem was that the properties in the models that were in the List did not have get/set on their public properties. Put another way, MVC3's automatic JSON binding only works on object properties that have get and set.

这将不绑定:

public string Str;

这将绑定:

public string Str { get; set; }