如何在MVC4中将Json字符串转换为C#对象 [英] How to Convert Json string into C# object in MVC4
本文介绍了如何在MVC4中将Json字符串转换为C#对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用Model字符串值属性在控制器上提交的json字符串.我想将此json字符串转换为c#通用列表对象.
I have a json string which is submitted on controller using Model string value property. I want to convert this json string into c# generic list object.
下面是我的json字符串
Below is my json string
{
"MLIDandRackPosition": [
{
"MLID": "27",
"PositionInRack": "3"
},
{
"MLID": "24",
"PositionInRack": "4"
}
]
}
下面是我的课程
public class MLIDandRackPosition
{
public int MLID { get; set; }
public int PositionInRack { get; set; }
}
我想要控制器动作方法上json字符串中的MLIDandRackPosition类的列表.
I want a List of MLIDandRackPosition class from json string on controller action method.
请帮助
推荐答案
您的对象将正确绑定到
public ActionResult SomeAction(IEnumerable<MLIDandRackPosition> MLIDandRackPosition)
如果设置以下ajax参数
If you set the following ajax parameters
var data = { "MLIDandRackPosition": [ { "MLID": "27", "PositionInRack": "3" }, { "MLID": "24", "PositionInRack": "4" } ] };
$.ajax({
...
traditional: true,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(data)
})
尽管我建议更改参数名称(例如model
)和var data = { "model": [ { "MLID": ....] };
although I would recommend changing the parameter name (to say model
) and var data = { "model": [ { "MLID": ....] };
这篇关于如何在MVC4中将Json字符串转换为C#对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文