我有一個json字符串,我想解析它到asp.net MVC控制器並綁定到我的模型。這是我做到的方式,但它不起作用(操作方法中的book參數變爲null)。請幫助將json字符串綁定到asp.net中的模型MVC
下面是我的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; }
}
非常感謝。我只是簡單地改變了操作方法,現在它可以正常工作。感謝您的解釋。 – Shyamal
非常抱歉,我修復了它 – Shyamal