我有来自knockoutjs的这个json数据
[{"Title":"test","Description":"tset","Price":"500.00","Status":"Reserved"},
{"Title":"s","Description":"d","Price":"400","Status":"Reserved"}]
如何在asp.net mvc 3中将其保存到我的控制器?
由于
答案 0 :(得分:4)
您可以将其作为AJAX请求发送:
var model = [{"Title":"test","Description":"tset","Price":"500.00","Status":"Reserved"}, {"Title":"s","Description":"d","Price":"400","Status":"Reserved"}];
$.ajax({
url: '/somecontroller/someaction',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(model),
success: function(result) {
}
});
然后你会有一个控制器动作来接收这个请求:
[HttpPost]
public ActionResult SomeAction(IEnumerable<MyViewModel> model)
{
...
}
其中MyViewModel
当然会反映JSON结构:
public class MyViewModel
{
public string Title { get; set; }
public string Price { get; set; }
public string Status { get; set; }
}