如何将列表从视图传递到控制器-MVC 4 [英] How to pass List from view to controller - MVC 4

查看:117
本文介绍了如何将列表从视图传递到控制器-MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将列表从视图传递到控制器时,我错过了一些小事情.在控制器的[HttpPost]方法中显示null.任何人都请指导我如何从视图到控制器获取列表数据.请在下面查看我的完整代码.

I missed a little thing while passing a list from view to controller. It displays null in [HttpPost] method of controller. Anyone please guide that how can I get list data from view to controller. Please view my complete code below.

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

在下面的代码中,我试图将提交的列表从视图发送到控制器,但结果为空.

In the following code i am trying to get the submitted list from view to controller but its result is null.

[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; }
}

推荐答案

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

With a foreach loop, MVC just creates the same inputs over and over again, because it doesn't know that they should be different. Therefore you need to use for-loops, and use indexes. I don't see any inputs in your form, so I'll just give you an example:

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

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

Also, if I remember correctly, you need to use an IList as model:

@model IList<payorder_draft_printing.Models.registration>

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

To be able to post your (non-editable) texts, you need to add hidden inputs:

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

这篇关于如何将列表从视图传递到控制器-MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆