将复杂的 JSON 映射到 MVC Action 参数中的列表
我有这样的类
public class someList
{
public string strOne {get; set;}
public string strTwo {get; set;}
}
public class Entity
{
public string EntityMember;
public List<someList> entityList;
}
然后我有一个控制器和操作,
public ActionResult MyControllerAction(Entity objEntity)
如何使用 JSON 数据从浏览器发布表单,以便它与我的实体对象映射。这是我当前用于提交表单的 javascript 函数,
function submitForm() {
var Entity = {};
var eList= new Array();
eList[0] = { strOne: "91", strTwo: "12" };
eList[1] = { strOne: "92", strTwo: "12" };
Entity = { EntityMember: "Member Value", entityList: eList };
$.post("/MyController/MyControllerAction", Entity);
}
但它不起作用。我能够获取 EntityMember 的值,但无法获取 List 对象。
有什么想法吗?
I have classes like this
public class someList
{
public string strOne {get; set;}
public string strTwo {get; set;}
}
public class Entity
{
public string EntityMember;
public List<someList> entityList;
}
Then I have a controller and action,
public ActionResult MyControllerAction(Entity objEntity)
How to post the form from browser with JSON data so that it maps with my Entity object. Here is my currenct javascript function for submitting the form,
function submitForm() {
var Entity = {};
var eList= new Array();
eList[0] = { strOne: "91", strTwo: "12" };
eList[1] = { strOne: "92", strTwo: "12" };
Entity = { EntityMember: "Member Value", entityList: eList };
$.post("/MyController/MyControllerAction", Entity);
}
But it is not working. I am able to get the value of EntityMember but the List object does not come.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是针对 asp.net mvc 3,则需要对实体进行 stringfied
如果这是针对 asp.net mvc 2,您还需要注册 json 提供程序,请参考此 link
你的不起作用的原因是jquery 的内置序列化格式已更改 http://api.jquery.com/jQuery.param/
您需要将其设置为使用传统方式:
jQuery.ajaxSettings.traditional = true;
或使用不同的序列化方法。 (在上面,我使用 stringify 函数进行 JSON 绑定,这是 JSON.org 的 json2.js 的一部分)
if this is for asp.net mvc 3, entity needs to be stringfied
if this is for asp.net mvc 2, you will also need to register the json provider, please reference this link
The reason yours doesn't work is that jquery's built-in serialization format has been changed http://api.jquery.com/jQuery.param/
you will need to either set it to use traditional way:
jQuery.ajaxSettings.traditional = true;
or use a different serialization method. (In above, I used stringify function for JSON binding, part of JSON.org's json2.js)