如何在MVC 4中从视图传递List到控制器

8

在从视图传递列表到控制器时,我错过了一个小细节。它在控制器的[HttpPost]方法中显示为null。请问有人能指导我如何从视图获取列表数据并传递到控制器吗?请查看我下面的完整代码。

@model List<payorder_draft_printing.Models.registration>

@{
    ViewBag.Title = "bulk_approval";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">

    <div class="row" style="text-align: left">

        <h2><u>Bulk Approval</u></h2>

        <br />
        <br />

        @using (Html.BeginForm("bulk_approval", "Sms", FormMethod.Post, new { enctype = "multipart/form-data" }))
        {
            <div style="width: 700px;" align="center">

                <table id="GetSerial" class="table">
                    <thead>
                        <tr class="ui-widget-header">
                            <th>Account Number</th>
                            <th>Mobile Number</th>
                            <th>Customer Name</th>
                            <th>Branch Code</th>
                            <th>Bulk Upload</th>
                            <th>Create Date</th>
                            <th>Created By</th>
                            <th>Active</th>
                           </tr>
                    </thead>

                    <tbody>

                        @if (Model != null)
                        {
                            foreach (var m in Model)
                            {
                            <tr style="height: 25px; border-bottom: 1px solid gray">
                                <td style="min-width: 120px">@m.account_number</td>
                                <td style="min-width: 120px; width: 450px;">@m.mobile_number</td>
                                <td style="min-width: 250px; width: 250px">@m.customer_name</td>
                                <td style="min-width: 100px; width: 100px">@m.BranchCode</td>
                                <td style="min-width: 100px; width: 100px">@m.BulkUpload</td>
                                <td style="min-width: 150px;">@string.Format("{0:dd-MMM-yyyy}", @m.create_date)</td>
                                <td style="min-width: 100px;">@m.created_by</td>
                                <td style="min-width: 100px; width: 100px">@m.Active</td>
                            </tr>
                            }
                        }

                    </tbody>

                </table>
                <input type="submit" value="Update" />
            </div>
        }

    </div>

</div>

在以下代码中,我正在尝试从视图获取提交的列表到控制器,但其结果为空。
[HttpPost]
public ActionResult bulk_approval(List<registration> model)//here my model shows null, please guide.
{
    foreach (var abc in model)
    {

    }
    return View();
}

以下是我的类。
public class registration
{
    public int Id { get; set; }
    public string mobile_number { get; set; }
    public string account_number { get; set; }
    public string customer_name { get; set; }
    public int FrequencyId { get; set; }
    public bool Active { get; set; }
    public string BranchCode { get; set; }
    public bool BulkUpload { get; set; }
    public string created_by { get; set; }
    public DateTime create_date { get; set; }
}

使用索引(for循环)代替foreach循环。 - devqon
请问您能否分享一些示例代码,谢谢。 - Rauf Abid
可能是HTML表格转ADO.NET DataTable的重复问题。 - user3559349
除了不能使用foreach循环(参见重复项),您还没有创建任何控件来编辑数据(使表单有点无意义)。 - user3559349
2个回答

14

使用foreach循环,MVC会一遍又一遍地创建相同的输入框,因为它不知道它们应该是不同的。因此,您需要使用for循环,并使用索引。我在您的表单中没有看到任何输入,因此我将给您一个例子:

@if (Model != null) {
    for (var i = 0; i < Model.Count; i++) {
        @Html.TextBoxFor(m => Model[i].SomeProperty)
    }
}

另外,如果我没记错的话,您需要使用 IList 作为模型:

@model IList<payorder_draft_printing.Models.registration>

为了能够发布您的(不可编辑的)文本,您需要添加隐藏的输入:

@Model[i].account_number 
@Html.HiddenFor(m => Model[i].account_number)

感谢您的澄清,现在我已经正确地获取到了值。现在我该如何以标签的形式显示这些值,而不是文本框呢?因为您在代码中使用了@Html.TextBoxFor。谢谢。 - Rauf Abid
2
为什么你想发布那些甚至不可编辑的内容?如果没有输入,表单的概念就会逐渐消失。 - devqon
你说得对,实际上我的情况是我只需要编辑最后一列[状态]的布尔值,无论它是真还是假,而其余列应该只显示为标签。请指导如何实现,谢谢。 - Rauf Abid
和以往的MVC一样,只是加上了索引:@Html.CheckBoxFor(m => m[i].Status)。对于显示,你可以直接使用 @m[i].account_number - devqon
感谢devqon,抱歉我仍然无法通过使用[@m [i] .account_number]得到所需的内容。 [@m]对象不可访问,因为删除了foreach(var m in Model)代码行。 这使得可以访问此对象。 在您提出的循环中,我该如何使其可访问? 谢谢。 - Rauf Abid
让我们在聊天中继续这个讨论 - Rauf Abid

0

我知道这是一个旧帖子,但我被卡在这里好几天了,所以我想在这里发布我的解决方案。第一个答案的代码大部分是正确的,但有一个重要的区别。在MVC中,您不能将IList或List从视图传递到控制器。

正如其他答案所指出的那样,您不能使用foreach,必须使用for()循环进行迭代,并使用m => Model[i].account_number引用索引。

其次,这就是让我卡住很长时间的原因,您不能传递IList<>或List。在后端创建一个仅包含对象列表的新模型,在上面的示例中,您可以将其命名为accountNumberList。此模型应包含公共帐户号码对象列表。创建后,修改控制器以将accountNumberList传递给视图,并将视图绑定到accountNumberList。最后,将Post方法接受accountNumberList作为参数。之后,您应该能够从视图检索数据列表,用户可以在单个提交中修改多个记录。

对于任何像我一样卡住的人,我希望这可以帮助到您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接