【问题标题】:How can I save from knockoutjs json list data to asp.net mvc 3 controller [closed]如何从 knockoutjs json 列表数据保存到 asp.net mvc 3 控制器 [关闭]
【发布时间】:2012-10-02 01:09:42
【问题描述】:

我有这个来自 knockoutjs 的 json 数据

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

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

谢谢

【问题讨论】:

    标签: asp.net-mvc json asp.net-mvc-3 knockout.js


    【解决方案1】:

    您可以将其作为 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; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-18
      • 1970-01-01
      • 2021-10-16
      • 2011-12-30
      • 2010-10-25
      • 2012-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多