2017-04-22 74 views
-1

我有方法的控制器如下:MVC5控制器未收到JSON對象

public JsonResult Save(List<BlogInfo> list) 
{ 
    return Json(new { Data = "" }, JsonRequestBehavior.AllowGet); 
} 

而且我從客戶端AJAX職位如下:

$.ajax({ 
    url: "Home/Save", 
    type: "post", 
    contentType: "application/json", 
    data: ko.mapping.toJSON(ViewModel), 
    success: function (response) { 
     alert(response.Status); 
    } 
}); 

我的問題是列表參數到控制器始終爲空。我試着將它改爲string而不是List,但也是null。

使用招,我可以看到,JSON是被通如下:

{"Data":[{"Id":1,"Title":"Sudoku Solver","Description":"Programmed Sudoku Solver","Tags":["programming","sudoku"]},{"Id":2,"Title":"Picnic","Description":"Went to millpoint","Tags":["picnic"]},{"Id":0,"Title":"Title","Description":"Description","Tags":[]}]} 
+1

請post hows看你的'數據' –

+3

'ko.mapping.toJSON(ViewModel)'產生了什麼?發貼數: –

+0

這就是我使用的Fiddler:'{「Data」:[{「Id」:1,「Title」:「Sudoku Solver」,「Description」:「程序化Sudoku求解器」,「Tags」編程「,」數獨「]},{」Id「:2,」標題「:」野餐「,」描述「:」去毫點「,」標籤「:[」野餐「]},{」Id「 0,「標題」:「標題」,「描述」:「描述」,「標籤」:[]}]}' –

回答

0

你已經表明並不代表陣列中的JSON,所以你不可能指望它綁定到一個列表服務器。爲了實現這一目標,確保你從客戶端發送對象的數組:

data: ko.mapping.toJSON(ViewModel.Data); 

下面我們就來了ViewModel.Data屬性,它表示一個數組,這樣我們只發送所需的JSON:

[{"Id":1,"Title":"Sudoku Solver","Description":"Programmed Sudoku Solver","Tags":["programming","sudoku"]},{"Id":2,"Title":"Picnic","Description":‌​"Went to millpoint","Tags":["picnic"]},{"Id":0,"Title":"Title","Description":"Description‌​","Tags":[]}] 
+0

完美,歡呼聲。 –