如何将List从视图传递到控制器 - MVC 4

Rau*_*bid 3 asp.net-mvc-4

从视图到控制器传递列表时我错过了一个小东西.它在控制器的[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>
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,我试图从视图到控制器获取提交的列表,但其结果为null.

[HttpPost]
public ActionResult bulk_approval(List<registration> model)//here my model shows null, please guide.
{
    foreach (var abc in model)
    {

    }
    return View();
}
Run Code Online (Sandbox Code Playgroud)

以下是我的课.

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; }
}
Run Code Online (Sandbox Code Playgroud)

dev*_*qon 7

通过foreach循环,MVC只是一遍又一遍地创建相同的输入,因为它不知道它们应该是不同的.因此,您需要使用for-loops,并使用索引.我没有在您的表单中看到任何输入,所以我只想举个例子:

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

另外,如果我没记错的话,你需要使用IListas模型:

@model IList<payorder_draft_printing.Models.registration>
Run Code Online (Sandbox Code Playgroud)

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

@Model[i].account_number 
@Html.HiddenFor(m => Model[i].account_number)
Run Code Online (Sandbox Code Playgroud)

  • 你为什么要发布甚至不可编辑的东西?如果您没有输入,表单的想法就会消失 (2认同)