任何人都可以引导我举例说明如何将传入的JSON转换为MVC3中的模型?ASP.Net MVC3 JSON到模型
2
A
回答
3
这已经由框架为您处理了。
所以你定义模型:
public class MyViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public Complex Complex { get; set; }
public IEnumerable<Foo> Foos { get; set; }
}
public class Complex
{
public int Id { get; set; }
}
public class Foo
{
public string Bar { get; set; }
}
然后控制器动作采取这种模式:
[HttpPost]
public ActionResult SomeAction(MyViewModel model)
{
...
}
最后你锤JSON请求匹配您的视图模型的结构此控制器操作:
$.ajax({
url: '@Url.Action("SomeAction")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
id: 1,
name: 'john smith of course, why asking?',
complex: {
id: 3
},
foos: [
{ bar: 'the bar' },
{ bar: 'the baz' },
]
}),
success: function(result) {
alert('hooray');
}
});
0
http://james.newtonking.com/projects/json-net.aspx
我将增加更多,但示例代码也是头版。
相关问题
- 1. 将JSON绑定到模型MVC3
- 2. MVC3模型DisplayFormat和JSON
- 3. ASP.NET MVC3模型验证
- 4. asp.net mvc3 razor模型验证
- 5. XML文件模型映射asp.net MVC3(动态地将一个xml文件映射到一个模型类asp.net mvc3)
- 6. ASP.NET MVC3:模型隐藏字段结合
- 7. ASP.Net MVC3更新相关的模型
- 8. 视图返回空模型asp.net MVC3
- 9. 当模型对象在MVC3(ASP.NET)创建
- 10. ASP.Net MVC3父子模型绑定
- 11. 编辑的ASP.NET MVC3模型绑定
- 12. ASP.Net MVC3模型绑定错误
- 13. ASP.NET MVC3与WCF作为模型
- 14. 生成模型在asp.net MVC3一个DropDownList
- 15. 将JSON数组绑定到ASP.NET MVC3
- 16. MVC3列表框内容到模型值
- 17. Asp.net MVC3传递一个动作链接模型中的模型
- 18. ASP.NET MVC3:DisplayTemplates不显示模型值(MVC3部分页面)
- 19. 无法上传文件ASP.NET MVC3。 XML文件到模型
- 20. ASP.NET MVC3如何从GET/POST将数据收集到模型中?
- 21. ASP.NET MVC3:使用jquery添加文本框并绑定到模型
- 22. 将ModelValidator插入到ASP.NET MVC3中的模型验证程序中
- 23. DataContract模型绑定到ASP.NET MVC中的JSON Action方法参数
- 24. MVC3 - 模型空岗
- 25. MVC3编辑模型
- 26. mvc3模型问题
- 27. MVC3强类型模型
- 28. ASP.NET MVC3 Automapper视图模型/型号查看验证
- 29. MVC3模型绑定到选择列表
- 30. MVC3绑定到收集数据模型
我们决定制作web服务SOAP。不需要JSON。 – MB34 2012-08-17 20:40:38