如何将List <model>从视图传递到控制器

时间:2018-07-25 16:37:55

标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 razor

作为ASP.NET MVC框架的新手,我正通过POST方法将模型列表发送回控制器。以下是我的源代码示例及其说明。

模型类别:

public class SiteIdentifier
    {
        [Display(Name = "Configuration Id")]
        public string ConfigurationId { get; set; }

        [Display(Name = "County")]
        public string County { get; set; }
    }

控制器(获取方法):

public ViewResult ViewNewSites()
{
    List<SiteIdentifier> sites = new List<SiteIdentifier>()
    {
        new SiteIdentifier{ConfigurationId = 1, County = "County1"},
        new SiteIdentifier{ConfigurationId = 2, County = "County2"},
        new SiteIdentifier{ConfigurationId = 3, County = "County3"}

    };

    return View(sites);
}

查看:

在此视图中,用户必须使用复选框选择一些行,然后单击提交按钮,然后提交按钮必须将那些选中的行传递给控制器​​。

@using HIE_Management_Studio.Common
@model IEnumerable<SiteIdentifier>

<!--If there is nothing to show, do not render this place.-->
@if (Model != null)
{
    using (Html.BeginForm("CopyToProductionPOST", "CopyBySite",
            new AjaxOptions
            {
                InsertionMode = InsertionMode.Replace,
                HttpMethod = "POST",
            }))
    {
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-info">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <table class="table table-bordered table-striped">
                    <thead>
                        <tr>
                            <th>
                                <input type="checkbox" id="chkSelectAll" />
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.ConfigurationId)
                            </th>
                            <th>
                                @Html.DisplayNameFor(model => model.County)
                            </th>
                        </tr>

                    </thead>
                    <tbody>
                        @foreach (var item in Model)
                        {
                            <tr>
                                <td class="text-nowrap">
                                    @Html.CheckBoxFor(modelItem => item.Selected, new { @class = "chkSelect" })
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.ConfigurationId)
                                    @Html.HiddenFor(modelItem => item.ConfigurationId)
                                </td>
                                <td>
                                    @Html.EditorFor(modelItem => item.County)
                                    @Html.HiddenFor(modelItem => item.County)
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <input type="submit" value="Submit" class="btn btn-info" />
            </div>
        </div>

        <script>
            $("#chkSelectAll").bind("change", function () {
                $(".chkSelect").prop("checked", $(this).prop("checked"));
            });
            $(".chkSelect").bind("change", function () {
                if (!$(this).prop("checked"))
                    $("#chkSelectAll").prop("checked", false);
            });
            $(".alert").hide().fadeIn("slow");
        </script>
    }
}

控制器(发布方法)

现在这是控制器中的post方法,该方法必须从视图中获取所有选定的行。

[HttpPost]
public ViewResult CopyToProductionPOST(List<SiteIdentifier> formCollection)
{
     SiteIdentifier siteIdentifier = new SiteIdentifier();
     // siteIdentifier.ConfigurationId = formCollection[0]["ConfigurationId"];

     return View("CopyBySite");
}

1 个答案:

答案 0 :(得分:0)