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

我有一个视图,其中包含模型中的项目列表。 我需要为每一行添加一个复选框,让用户选中多个复选框,并将选择了哪一行的标识符传递给控制器​​。 我知道如何通过动作链接传递单个值,但我不确定如何通过动作链接传递多个值或如何“收集”选择哪些行。 我将在下面展示我的一些代码尝试。 有人可以帮我理清为什么我无法获得传递给控制器​​的所有复选框的值吗?

这是我的页面

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

我需要用户做的是选择第1行和第3行(例如)并将这些应用程序ID传递给控制器​​。

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

当命中控制器时,参数int为空白。 如何从复选框中获取值? 我使用这个站点的基本思想是命名所有复选框,并通过动作链接传递ICollection: http : //haacked.com/archive/2008/10/23/model-binding-to-a-list。 ASPX /

视图:

 @model PagedList.IPagedList 

Merchant Application Report

@foreach (var item in Model) {
@Html.ActionLink(item.ApplicationID.ToString(), "ViewApplication", new { ID = item.ApplicationID, edit = 1 }, new AjaxOptions { HttpMethod = "GET" }) @Convert.ToDateTime(item.ApplicationDate).ToString("M/d/yy") @item.ApplicantName
@Html.ActionLink("Print Application", "PrintApplication", "CreateContract", new { @class = "btn btn-primary" })

控制器:

  [AuthorizeAdmin] public ActionResult PrintApplication(ICollection 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传递到您的页面,然后使用模型绑定器将相同的视图模型发布回控制器

楷模:

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

控制器:

 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 list = new List(); 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; }     Index   
@for (int i = 0; i < Model.Merchants.Count; i++) { }
@Html.CheckBoxFor(x => x.Merchants[i].IsSelected) @Html.HiddenFor(x => x.Merchants[i].AppId) @Model.Merchants[i].AppId @Html.HiddenFor(x => x.Merchants[i].Name) @Model.Merchants[i].Name

回到控制器中的[HttpPost]方法,您将获得一个商家模型列表,其中bool值为true或false。 通过这种方式,您可以检查它是否属实,并从那里抓取AppId。

不要在mvc中使用foreach ,总是使用for和索引变量进行迭代。

你还需要一个bool来跟踪选择状态。

这个示例代码适用于我:

 public class AModel { public List Items { get; set; } } public class AnotherModel { public int ApplicationId { get;set; } public DateTime ApplicationDate { get; set; } public string ApplicantName { get; set; } public bool Selected { get; set; } } 

Page.cshtml

 @using (Html.BeginForm("PostIndex", "Home", FormMethod.Post)) {  @for (var i = 0; i < Model.Items.Count(); i++) {  } 
@Html.ActionLink(Model.Items[i].ApplicationId.ToString(), "ViewApplication", new {ID = Model.Items[i].ApplicationId, edit = 1}, new AjaxOptions {HttpMethod = "GET"}) @Html.DisplayFor(model => model.Items[i].ApplicationDate) @Html.DisplayFor(model => model.Items[i].ApplicationId)
}

@CBauer和@NickYoung基本上都有答案。 密钥是(1)确保我的复选框的ID用括号中的序号写入相同的后缀,(2)使用提交按钮而不是操作链接,以及(3)正确编写控制器以接受ID从复选框。 以为我会发布一些代码来让你了解我的工作原理:

查看(部分):

 @model PagedList.IPagedList ... @{var i = 0;} @foreach (var item in Model) { var tmp = "[" + i + "].ints";     ...  ... 

控制器:

 [AuthorizeAdmin] [HttpPost] public ActionResult MerchantApplicationSummary(string[] ints, FormCollection form, int? page) { ... 

感谢@CBauer和@NickYoung的帮助!