通过 JSON 将对象数组发布到 ASP.Net MVC3 [英] Post an Array of Objects via JSON to ASP.Net MVC3

查看:22
本文介绍了通过 JSON 将对象数组发布到 ASP.Net MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找通过 JSON 将对象数组发布到 MVC3 的解决方案.

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

我正在处理的示例代码:http://weblogs.asp.net/scottgu/archive/2010/07/27/introducing-asp-net-mvc-3-preview-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
}

本次POST的结果:

list 被设置为一个 ListViewModel
其 X 和 Y 属性已设置
底层 ItemList 属性已设置
ItemList 包含一项,因为它应该
该 ItemList 中的项目未初始化.Str 为空,启用为假.

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?

我已经尝试过但无济于事的相关 StackOverflow 问题:

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

Asp.net Mvc Ajax Json(后数组)使用 MVC2 和更旧的基于表单的编码 - 该方法因包含对象数组的对象而失败(JQuery 无法对其进行正确编码).

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).

发布一组带有 JSON、JQuery 到 ASP.NET MVC 控制器的复杂对象使用我想避免的 hack,Controller 接收一个普通字符串,然后手动反序列化自己,而不是利用框架.

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 后绑定无法正常工作没有设置他的内容类型 - 它在我的代码中设置.

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

如何使用 JSON、jQuery 将复杂对象数组发布到 ASP.NET MVC 控制器?这个可怜的家伙不得不编写一个 JsonFilter 来解析一个数组.另一个我宁愿避免的黑客行为.

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.

那么,我该如何实现?

推荐答案

问题是 List 中模型中的属性在其公共属性上没有 get/set.换句话说,MVC3 的自动 JSON 绑定仅适用于具有 get 和 set 的对象属性.

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; }

这篇关于通过 JSON 将对象数组发布到 ASP.Net MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆