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