【问题标题】:How to get checkbox selected value and pass to controller in asp.net mvc 4如何在asp.net mvc 4中获取复选框选定值并传递给控制器
【发布时间】:2015-02-11 03:03:26
【问题描述】:

我正在尝试获取选中的复选框值

这是我的模型,

public class VehicleViewModel : Vehicle
{
    [Display(Name = "Vehicle Type")]
    [Required( ErrorMessage = "{0} is required.")]
    public string VehicleTypeName { get; set; }

    [Display(Name = "Location")]
    [Required(ErrorMessage = "{0} is required.")]
    public string LocationName { get; set; }

    public IEnumerable<AssignProductsViewModel> AssignedProducts { get; set; }
}

public class AssignProductsViewModel
{
    public long ProductID { get; set; }
    public string ProductName { get; set; }
}

这是我的观点

@foreach (var item in Model.AssignedProducts)
{
  <tr>
    <td>
      <input type="checkbox" value ="@item.ProductID"/>
    </td>
    <td>
      @Html.DisplayFor(model => item.ProductName)
    </td>
  </tr>
}

这是我的控制器

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewVehicle(VehicleViewModel vehicleViewModel, string selected)
{
  //Do something with the string here
  return View();
}

我知道我需要使用 javascript 将选中的复选框值传递给字符串并将字符串传递给控制器​​。但我不知道该怎么做,因为我是 javascript 和 MVC 的新手。

【问题讨论】:

  • 您的复选框甚至没有name 属性,因此不会回发。您还使用重复的 id 属性创建了无效的 html。然后您需要将参数设为数组,例如string[] selectedCheckboxes 尽管属性 ProductID 暗示它可能是 int 无论如何。首先查看jquery ajax documentation,了解如何使用 ajax 发帖。
  • 您总是使用 FormCollection。 stackoverflow.com/questions/21303236/…
  • @TimSouthard ,它适用于桌子吗?我需要能够检查多个复选框。谢谢。 :)
  • 它适用于任何标签,只要您使用表单。
  • 不要使用FormCollection。您应该始终绑定到模型并让 MVC 为您处理绑定和验证。您是否有理由需要使用 ajax 发布而不是标准表单提交?

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


【解决方案1】:

基于不需要 ajax 帖子的 cmets,您的 AssignProductsViewModel 需要一个附加属性来绑定复选框

public class AssignProductsViewModel
{
  public long ProductID { get; set; }
  public string ProductName { get; set; }
  public bool IsSelected { get; set; } // add this
}

在视图中,使用for 循环或自定义EditorTemplate 来呈现集合是否使用索引器正确命名控件。 foreach 循环生成重复的 id(无效 html)和 name 属性(不能绑定到集合)

@model VehicleViewModel
@using(Html.BeginForm())
{
  // controls for VehicleTypeName, LocationName
  for(int i = 0; i < Model.AssignedProducts.Count; i++)
  {
    @Html.HiddenFor(m => m.AssignedProducts[i].ProductID) // ditto for ProductName if you want it on postback
    @Html.CheckBoxFor(m => m.AssignedProducts[i].IsSelected)
    @Html.LabelFor(m => m.AssignedProducts[i].IsSelected, Model.AssignedProducts[i].ProductName)
  }
  ....
}

然后发回给

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult NewVehicle(VehicleViewModel model)
{
  // model.AssignedProducts contains the collection with a value indicating if the product has been selected
}

或者,您可以使用 EditorTemplate 类型的 AssignProductsViewModel 来呈现集合

/Views/Shared/EditorTemplates/AssignProductsViewModel.cshtml

@model AssignProductsViewModel
@Html.HiddenFor(m => m.ProductID) // ditto for ProductName if you want it on postback
@Html.CheckBoxFor(m => m.IsSelected)
@Html.LabelFor(m => m..IsSelected, Model.ProductName)

在主视图中

@model VehicleViewModel
@using(Html.BeginForm())
{
  // controls for VehicleTypeName, LocationName
  @Html.EditorFor(m => m.AssignedProducts)
  <input type="submit" />
}    

【讨论】:

  • 我尝试了你的代码,但我在 for 循环中遇到错误:CS0019:运算符 '
  • 糟糕,没有注意到您的属性是IEnumerable。它必须是IList&lt;AssignProductsViewModel&gt;。但是,如果您不能更改它,那么您可以使用自定义 EditorTemplate(无论如何我都会更新答案以添加该选项)
猜你喜欢
  • 1970-01-01
  • 2012-12-31
  • 1970-01-01
  • 2013-11-23
  • 2014-12-29
  • 1970-01-01
  • 2023-03-25
  • 2015-07-23
  • 1970-01-01
相关资源
最近更新 更多