c# - 在 MVC/Razor 中,如何获取多个复选框的值并将它们全部传递给 Controller ​​?

标签 c# asp.net-mvc asp.net-mvc-4 razor checkbox

我有一个 View ,其中包含模型中的项目列表。我需要在每一行中添加一个复选框,让用户选择多个复选框,然后将所选行的一些标识符传递给 Controller ​​。我知道如何通过操作链接传递单个值,但我不确定如何通过操作链接传递多个值或如何“收集”选择了哪些行。我将在下面展示我的一些代码尝试。谁能帮我弄清楚为什么我无法获取传递给 Controller ​​的所有复选框的值?

这是我的页面

Checkbox     App ID     Date     Name
   []          1        5/10     Bob
   []          2        5/10     Ted
   []          3        5/11     Alice

我需要用户做的是选择第 1 行和第 3 行(例如)并将这些 App ID 传递给 Controller ​​。

我开始列出各种尝试,但决定只展示我当前的尝试,看看是否有人可以指出我做错了什么。我看到的在线示例和我的示例之间的主要区别在于,我的示例使用 PagedList 并在 foreach 循环中创建表的行。

参数 ints 打到 Controller 上时为空。如何从复选框中获取值?我使用这个站点的基本思想是将所有复选框命名为相同的并通过操作链接传递 ICollection: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx/

查看:

@model PagedList.IPagedList<CarmelFinancialWeb.Models.ModelMerchantSummary>
<div class="block" style="width: 100%; float: left">
<p class="block-heading"> 
Merchant Application Report
</p>
<div class="table-holder">
    <table class="table" style="margin-bottom: 0px">
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            <input type="checkbox" name="ints" value=item.ApplicationID />
                        </td>
                    <td>
                        @Html.ActionLink(item.ApplicationID.ToString(), "ViewApplication", new { ID = item.ApplicationID, edit = 1 }, new AjaxOptions { HttpMethod = "GET" })
                    </td>
                    <td>
                        @Convert.ToDateTime(item.ApplicationDate).ToString("M/d/yy")
                    </td>
                    <td>
                        @item.ApplicantName
                    </td>
        </tbody>
    </table>
</div>
</div>
@Html.ActionLink("Print Application", "PrintApplication", "CreateContract", new { @class = "btn btn-primary" })

Controller :

    [AuthorizeAdmin]
    public ActionResult PrintApplication(ICollection<int> ints, string ID)
    {
        Contracts_Create contract = new Contracts_Create();
        ModelApplication currentApplication = new ModelApplication();
        currentApplication.contract = new ModelContract();
        return File(contract.CreatePDF_PrintedApplication_English(currentApplication.contract.location, currentApplication.contract), "application/pdf");
    }

编辑: 这被标记为另一个问题的副本。那里的问题是关于是否可以使用非顺序输入名称。我的问题是我使用的输入不是非连续的,但它仍然无法正常工作。我理解这个概念,只是想不通为什么我的特定代码不起作用。我花了很多时间来解决这个问题,但找不到我的特定代码的答案。谢谢!

最佳答案

尝试将 ViewModel 传递到您的页面,并使用模型绑定(bind)器将相同的 View 模型发布回您的 Controller

模型:

public class MerchantModel
{
    public int AppId { get; set; }
    public string Name { get; set; }
    public bool IsSelected { get; set; }
}

public class MerchantViewModel
{
    public List<MerchantModel> Merchants { get; set; }
}

Controller :

public class DefaultController : Controller
{
    // GET: Default
    public ActionResult Index()
    {
        var merchant1 = new MerchantModel
        {
            AppId = 1,
            Name = "Bob"
        };
        var merchant2 = new MerchantModel
        {
            AppId = 2,
            Name = "Ted"
        };
        var merchant3 = new MerchantModel
        {
            AppId = 3,
            Name = "Alice"
        };

        List<MerchantModel> list = new List<MerchantModel>();
        list.Add(merchant1);
        list.Add(merchant2);
        list.Add(merchant3);

        var model = new MerchantViewModel
        {
            Merchants = list
        };

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MerchantViewModel model)
    {
        return View(model);
    }
}

查看:

 @model TestCheckBoxes.Models.MerchantViewModel

    @{
        Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div> 
        <form action="/default/index" method="post">
            <table>
                @for (int i = 0; i < Model.Merchants.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.CheckBoxFor(x => x.Merchants[i].IsSelected)
                        </td>
                        <td>
                            @Html.HiddenFor(x => x.Merchants[i].AppId)
                            @Model.Merchants[i].AppId
                        </td>
                        <td>
                            @Html.HiddenFor(x => x.Merchants[i].Name)
                            @Model.Merchants[i].Name
                        </td>
                    </tr>
                }
            </table>

            <button type="submit">Submit</button>
        </form>



    </div>
</body>
</html>

回到 Controller 中的 [HttpPost] 方法,您将得到一个 MerchantModel 的列表,其 bool 值要么是 true 要么是 false。这样您就可以检查它是否为真并从那里获取 AppId。

关于c# - 在 MVC/Razor 中,如何获取多个复选框的值并将它们全部传递给 Controller ​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199602/

相关文章:

asp.net-mvc-4 - 提交表单时 MVC : System. NullReferenceException 模型

@section 中的 Javascript for 循环导致解析器错误

c# - 使用企业库异常 block 的 WCF 异常屏蔽

c# - Visual Studio : Setting a conditional breakpoint without setting an unconditional one first

c# - 在 Chrome 浏览器中运行测试时,编码的 Ui 播放无法找到给定的搜索控件

c# - DotNetZip 从 MemoryStream 打开 zip 文件

c# - 如何禁用 MARS 并规避 "MARS is not yet implemented"-exception”?

.net - 一些(官方或非官方)将 Mvc3 升级到 Mvc5 指南?

c# - 无法加载文件或程序集 'DotNetOpenAuth.Core

c# - 我将如何在 ASP.Net 服务器上运行连续任务?