Shyamal
Shyamal

Reputation: 161

binding json string to a model in asp.net MVC

I have a json string and I want to parse it to the ASP.NET MVC controller and bind to my model. Here is the way I did it but its not working, because the book parameter in the action method is null.

Below is my json string:

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);
        }
    });
}

Below is my action method:

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

Below is my model:

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; }
}

Upvotes: 2

Views: 5524

Answers (2)

adiga
adiga

Reputation: 35261

The DefaultModelBinder is unable to bind your object because it expects a payload like this:

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

So, the json should be

"{\"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}]}}";

The json you have created would work if the action method was like this:

public ActionResult book(book[] book)
{
}

To avoid this confusion with having too many books, you can change the parameter of the Action method to:

public ActionResult book(model model)
{
}

and the json to:

"{\"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}]}}";

Another way to do this would be to add FromBody attribute to the parameter like this:

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

You'd need to add a System.Web.Http reference to use this attribute.

Upvotes: 2

Rahul Mistry
Rahul Mistry

Reputation: 192

 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}]}";

first you needs to unescape which will gives you in format of json

In 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);
        }
    });

Controller

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

Models

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; }
    }

Upvotes: 0

Related Questions