将json字符串绑定到asp.net MVC中的模型

时间:2017-10-14 06:45:56

标签: json asp.net-mvc

我有一个json字符串,我想将它解析为ASP.NET MVC控制器并绑定到我的模型。这是我做的方式,但它不起作用,因为action方法中的book参数是null

下面是我的json字符串:

function parseDAta() {
    var jsonString = "{\"book\":[{\"id\":\"01\",\"author\":\"j.k.rowling\",\"price\":250,\"available\":true,\"editions\":[{\"id\":\"001\",\"name\":\"2017\"},{\"id\":\"002\",\"name\":\"2018\"}]},{\"id\":\"02\",\"author\":\"carlsom james\",\"price\":500,\"available\":false,\"editions\":null}]}";

    $.ajax({
        url: '/book/book',
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: jsonString,
        success: function (result) {
            console.log('Data received: ');
            console.log(result);
        }
    });
}

以下是我的行动方法:

[HttpPost]
public ActionResult book(model book)
{
    return null;
}

以下是我的模特:

public class model
{
    public book[] book { get; set; }
}

public class book
{
    public string id { get; set; }
    public string availabity { get; set; }
    public string Author { get; set; }
    public int price { get; set; }

    public  edition[] edition { get; set; }
}

public class edition
{
    public string name { get; set; }
    public string id { get; set; }
}

2 个答案:

答案 0 :(得分:1)

DefaultModelBinder无法绑定您的对象,因为它需要这样的有效负载:

{"book": {"book":[]}}

所以,json应该是

"{\"book\":{\"book\":[{\"id\":\"01\",\"author\":\"j.k.rowling\",\"price\":250,\"available\":true,\"editions\":[{\"id\":\"001\",\"name\":\"2017\"},{\"id\":\"002\",\"name\":\"2018\"}]},{\"id\":\"02\",\"author\":\"carlsom james\",\"price\":500,\"available\":false,\"editions\":null}]}}";

如果动作方法是这样的话,你创建的json会起作用:

public ActionResult book(book[] book)
{
}

为避免与book太多的混淆,您可以将Action方法的参数更改为:

public ActionResult book(model model)
{
}

和json:

"{\"model\":{\"book\":[{\"id\":\"01\",\"author\":\"j.k.rowling\",\"price\":250,\"available\":true,\"editions\":[{\"id\":\"001\",\"name\":\"2017\"},{\"id\":\"002\",\"name\":\"2018\"}]},{\"id\":\"02\",\"author\":\"carlsom james\",\"price\":500,\"available\":false,\"editions\":null}]}}";

另一种方法是将FromBody属性添加到参数中,如下所示:

public ActionResult book([FromBody]model book)
{
}

您需要添加System.Web.Http引用才能使用此属性。

答案 1 :(得分:0)

 var jsonString = "{\"book\":[{\"id\":\"01\",\"author\":\"j.k.rowling\",\"price\":250,\"available\":true,\"editions\":[{\"id\":\"001\",\"name\":\"2017\"},{\"id\":\"002\",\"name\":\"2018\"}]},{\"id\":\"02\",\"author\":\"carlsom james\",\"price\":500,\"available\":false,\"editions\":null}]}";

首先你需要unescape,它将以json

的格式提供给你

在JS中

var jsonString =unescape("{\"book\":[{\"id\":\"01\",\"author\":\"j.k.rowling\",\"price\":250,\"available\":true,\"editions\":[{\"id\":\"001\",\"name\":\"2017\"},{\"id\":\"002\",\"name\":\"2018\"}]},{\"id\":\"02\",\"author\":\"carlsom james\",\"price\":500,\"available\":false,\"editions\":null}]}");
    console.log(jsonString);
    $.ajax({
        url: '/home/book',
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: jsonString,
        success: function (result) {
            console.log('Data received: ');
            console.log(result);
        }
    });

<强>控制器

        [HttpPost]
        public ActionResult book(List<book> book)
        {
            return null;
        }

<强>模型

public class model //No need of this class,you can remove it
    {
        public book[] book { get; set; }

    }

    public class book
    {
        public string id { get; set; }
        public string availabity { get; set; }
        public string Author { get; set; }
        public int price { get; set; }

        public edition[] edition { get; set; }

    }

    public class edition
    {
        public string name { get; set; }
        public string id { get; set; }
    }