How to pass a list of objects to a [HTTPPost]controller parameter, in ASP.Net MVC?
-
30-06-2021 - |
Question
i have the next view:
@model IEnumerable<L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer>
@using (Html.BeginForm("ExpenseMonthlyTransferProcessing", "BudgetTransfer", Model.ToList())){
<table class ="divTable">
<tr>
<th>Transferir</th>
<th>
Clave
</th>
<th>
Monto
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.CheckBoxFor(x => item.MTR_Bool, new { @class = "checkMTR", @checked = "checked" })
</td>
<td>
@Html.TextBoxFor(x => item.MTR_Key, new {@class = "longInput" })
</td>
<td>
@String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
}
</table>
}
and my controller like this
[HttpPost]
public ActionResult ExpenseMonthlyTransferProcessing(List<MTR_MonthlyTransfer> lstMtr)
{ return View(lstMTR); }
But when i do the post my list is null, how can i send my list through the submit button ?
Solution
You should change the @model
to an array (L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]
) or something else that implements IList<>
:
@model L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]
@for (var i = 0; i < Model.Length; i ++) {
<tr>
<td>
@Html.CheckBoxFor(x => Model[i].MTR_Bool, new { @class = "checkMTR", @checked = "checked" })
</td>
<td>
@Html.TextBoxFor(x => Model[i].MTR_Key, new {@class = "longInput" })
</td>
<td>
@String.Format("{0:F}", item.MTR_Amount)
</td>
</tr>
OTHER TIPS
receive a FormCollection and parse the items in it manually
Use F12 to check the post in your navigator to see if it are sending the content you expected.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow