0

我有来自 knockoutjs 的这个 json 数据

[{"Title":"test","Description":"tset","Price":"500.00","Status":"Reserved"}, 
 {"Title":"s","Description":"d","Price":"400","Status":"Reserved"}]

如何将其保存到我在 asp.net mvc 3 中的控制器中?

谢谢

4

1 回答 1

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; }
}
于 2012-10-11T11:28:45.380 回答