我正在用AJAX提交JQuery的一些數據(Collection<Cars>
),但現在我不知道如何在我的Action輸入參數中定義對象。我已經嘗試將其定義爲FormCollection並且我收到了所有數據。不過我真正想要的是定義爲ICollection<Car>
但是當我這樣做時我的對象變爲空。我怎樣才能做到這一點?或者我怎麼能解析一個FormCollection到ICollection<Car>
?定義輸入參數的名稱
我的控制器:
public ActionResult UpdateTable(ICollection<Car> collection)
{
//some code
}
我的JS:
function updateTable(tableId) {
if (tableId == "myTable")
{
event.preventDefault();
if ($("#" + tableId + " tbody tr").length % 2 == 0)
var myTableData = $("#" + tableId).find("select, input").serializeObject();
$.post("../../Car/UpdateTable/", myTableData , function(partial) {
$("#myOtherTable tbody").append(partial);
});
}
}
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
Car.cs:
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Owner> Owners { get; set; }
public bool IsSold { get; set; }
個結果:
當輸入參數是ICollection<Car>
什麼來動作控制器:空
當輸入參數爲FormCollection
(在AllKeys)什麼來動作控制器:
Car[0].Id
Car[0].Name
Car[0].Owners
IsSold
我查看(行表):
<tr>
<td>
@Html.TextBoxFor(model => model.Id, new { Name = modelFieldInitialName + "Id", id = modelFieldInitialId + "Id" })
@Html.ValidationMessageFor(model => model.Id)
</td>
<td>
@Html.TextBoxFor(model => model.Name, new { Name = modelFieldInitialName + "Name", id = modelFieldInitialId + "Name" })
@Html.ValidationMessageFor(model => model.Name)
</td>
<td>
@Html.DropDownListFor(model => model.Owners, new SelectList(TempData["OwnersList"] as IEnumerable, "Id", "Description"), "", new { Name = modelFieldInitialName + "Owners", id = modelFieldInitialId + "Owners" })
@Html.ValidationMessageFor(model => model.Owners)
</td>
<td>
@Html.CheckBoxFor(model => model.IsSold, new { type = "checkbox", Name = modelFieldInitialName + "IsSold", id = modelFieldInitialId + "IsSold" })
@Html.ValidationMessageFor(model => model.IsSold)
</td>
</tr>
你試圖發送一個JSON? –
@Guillelon我該怎麼做?我從來沒有與JSON合作 – Ninita
不能用你的HTML表格和我的汽車類定義 –