在ASP.NET MVC 4中接受JSON
我将一个JSON发送到ASP.NET的控制器函数,JSON如下所示:在ASP.NET MVC 4中接受JSON,asp.net,asp.net-mvc,json,Asp.net,Asp.net Mvc,Json,我将一个JSON发送到ASP.NET的控制器函数,JSON如下所示: [{"Key":"Test23.txt","Size":6,"LastModified":"Thu, 01 Nov 2012 19:35:43 GMT"}, {"Key":"Test25.txt","Size":6,"LastModified":"Wed, 31 Oct 2012 21:02:51 GMT"}, {"Key":"Test28.txt","Size":6,"LastModified":"Thu, 01 Nov 2
[{"Key":"Test23.txt","Size":6,"LastModified":"Thu, 01 Nov 2012 19:35:43 GMT"},
{"Key":"Test25.txt","Size":6,"LastModified":"Wed, 31 Oct 2012 21:02:51 GMT"},
{"Key":"Test28.txt","Size":6,"LastModified":"Thu, 01 Nov 2012 19:35:42 GMT"}]
我如何在MVC控制器方法中接受它,以及如何解析它
谢谢。如果匹配,Mvc将自动将其绑定到viewModel
public ActionResult SaveFiles(List<FileViewModel> filesViewModel){
}
公共类项目
{
公共字符串密钥{get;set;}
公共整数大小{get;set;}
公共日期时间LastModified{get;set;}
}
公共操作结果MyAction(列表项)
{
}
默认型号活页夹可以为您完成所有这些操作。不需要使用任何第三方库。将集合传递到操作时,它们确实需要是可索引的。因此,您必须使用数组
或列表
// First, define your input model.
public class MyModel
{
public string Key { get; set; }
public int Size { get; set; }
public string LastModified { get; set; }
}
// NExt, use that model in your action.
public ActionResult MyAction(MyModel[] things)
{
}
你可以用它来解析。你控制生成这个JSON和发出服务器请求的过程吗?我有一些控制权,我正在开发前端,它使用ExtJs,然后生成JSON。不,我使用空的MVC4项目,没有“默认模型绑定器”。是的,有。。。
public class Item
{
public string Key {get;set;}
public int Size {get;set;}
public DateTime LastModified {get;set;}
}
public ActionResult MyAction(List<Item> items)
{
}
// First, define your input model.
public class MyModel
{
public string Key { get; set; }
public int Size { get; set; }
public string LastModified { get; set; }
}
// NExt, use that model in your action.
public ActionResult MyAction(MyModel[] things)
{
}