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

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:

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

ItemViewModel.cs:

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

ListController.反恐精英:

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

The result of this POST:

列表设置为ListViewModel
设置其X和Y属性
基础ItemList属性已设置
ItemList包含一个项目,因为它应该
该ItemList中的项未初始化.字符串为空,启用为假.

换句话说,这是我从MVC3的模型绑定中得到的:

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

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?

我已经追问过的相关问题都无济于事:

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 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 Binding not working correctly

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?

推荐答案

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.

This will not bind:

public string Str;

This will bind:

public string Str { get; set; }

Json相关问答推荐

JSON:将项';S键/名称移动到属性中,并使用JQ将其转换为数组

盒子图显示不正确

需要有关在Ffltter应用程序中解码JSON的帮助;未处理的异常:类型不是类型转换中类型的子类型

PowerShell:使用JSON原生的Short命令处理JSON?

使用快速json库编写json可以消除所有缩进

在深度嵌套数组中使用布尔属性的jq-select

如何在 jq 中按 IP 地址排序?

Jolt 变换 - 如何用字段值重命名字段?

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

Rails:format.js 或 format.json,或两者兼而有之?

在 Apache Spark 中读取多行 JSON

IE中Json响应下载(7~10)

Select 什么数据类型json或者jsonb或者text

在 React 中访问子级的父级状态

在 HTML 数据属性上添加 JSON 是不是很糟糕?

如何在 Python 中合并两个 json 字符串?

NSManagedObject 属性值的 NSNull 处理

获取一个数字的 PHP 对象属性

确保数组中的项目属性在 Json Schema 中是唯一的?

从调试器获取 IntelliJ Idea 中的 JSON 对象