任何人都可以引導我舉例說明如何將傳入的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