【问题标题】:How to pass a list of objects to a [HTTPPost]controller parameter, in ASP.Net MVC?如何在 ASP.Net MVC 中将对象列表传递给 [HTTPPost] 控制器参数?
【发布时间】:2012-09-03 22:21:05
【问题描述】:

我有下一个观点:

@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> 
}

和我的控制器这样

[HttpPost]
    public ActionResult ExpenseMonthlyTransferProcessing(List<MTR_MonthlyTransfer> lstMtr)
    { return View(lstMTR); }

但是当我发布我的列表时,我的列表为空,我如何通过提交按钮发送我的列表?

【问题讨论】:

    标签: asp.net-mvc list submit http-post asp.net-mvc-controller


    【解决方案1】:

    您应该将 @model 更改为数组 (L5ERP.Model.BLL.BusinessObjects.MTR_MonthlyTransfer[]) 或实现 IList&lt;&gt; 的其他内容:

    @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> 
    

    【讨论】:

    • Ed 指的是您需要索引模型绑定器的名称这一事实。当他的示例呈现时,MTR_Key 文本框看起来像 这使模型绑定器能够知道每个输入属于数组中的哪个元素。或者,将其作为 json 对象数组提交应该可以。
    【解决方案2】:

    接收 FormCollection 并手动解析其中的项目

    使用 F12 检查导航器中的帖子,看看它是否发送了您期望的内容。

    【讨论】:

    • 我用 FOR 而不是 FOREACH 填充我的表,但我在 mi 模型中使用 .ToList,现在这样做我可以访问我收藏的 de 索引。这是我的观点的代码: @for (int i = 0; i } 这行得通;)!
      Transferir Clave Monto
      @Html.TextBoxFor(x => Model.ToList()[i].MTR_Key)
    猜你喜欢
    • 1970-01-01
    • 2010-09-14
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多