【问题标题】:How to pass List<Model> from view to controller [duplicate]如何将 List<Model> 从视图传递到控制器 [重复]
【发布时间】:2019-01-02 13:33:42
【问题描述】:

作为 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");
}

【问题讨论】:

  • @fawad 请检查重复的答案
  • @TAHASULTANTEMURI 这个答案对我不起作用,对问过的人也不起作用。
  • 那么请你发帖这个“SiteIdentifier”类吗?
  • @TAHASULTANTEMURI 我刚刚在我的问题中添加了模型类。

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


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
相关资源
最近更新 更多