2014-09-01 38 views
0

我想用$.ajax發回表格。阿賈克斯數據將有表格數據,並且還有一個名爲ItemList的附加列表。我使用$.extend(請參閱下面的代碼)將項目列表數組附加到序列化表單中,然後在將表單對象傳遞給ajax post調用之前對其進行字符串化。 控制器操作正在接收所有數據,除了itemList(項目類型列表)屬性爲null外。我檢查了項目對象屬性名稱是否正確匹配,使用Json傳入的名稱。任何想法如何將json數組綁定到模型內部的IList屬性? (MVC)

目標控制器

[Authorize] 
    [HttpPost] 
    public ActionResult Add(AddQuoteRequestVM model) 
    { 
     .. code here 
    } 

AddQuoteRequestVM

public class AddQuoteRequestVM 
    { 

     public ParcelVM ItemDescription { get; set; } 
      .. other complex type properties 
    } 

ParcelVM

public class ParcelVM 
{ 
    [Required] 
    public IList<Item> ItemList { get; set; } 
} 

Ajax調用

form = jQuery('#createQuoteForm').serializeObject(); 
    var itemList = $.parseJSON(ko.toJSON(viewModel.ItemList())); 

    $.extend(form, { ItemList : itemList}); 


    var data = JSON.stringify({ model: form }); 

     $.ajax({ 
      url: "Add", 
      type: "POST", 
      data: data, 
      contentType: 'application/json; charset=utf-8', 

    }); 

回答

0

如果你有一個模型嵌套在另一個模型中,那麼jquery序列化程序很難將它映射到一個級別。我曾經遇到同樣的問題。 嘗試下面的解決方法並檢查IList對象是否仍爲空。

[Authorize] 
    [HttpPost] 
    public ActionResult Add(AddQuoteRequestVM model, IList<Item> ItemList) 
    { 
     .. code here 
    } 
相關問題