2017-10-14 99 views
1

我有一個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; } 
 
    }

回答

0

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 s此混淆,您可以操作方法的參數更改爲:

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參考使用此屬性。

+0

非常感謝。我只是簡單地改變了操作方法,現在它可以正常工作。感謝您的解釋。 – Shyamal

+0

非常抱歉,我修復了它 – Shyamal

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

首先你需要取消轉義這將讓你在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); 
     } 
    }); 

精讀的格式troller

 [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; } 
    } 
+1

非常感謝。我按照你的解釋做了改變,這是行得通的。感謝您的解釋。 – Shyamal