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',
});