简体   繁体   English

如何获取复选框选定的值并传递给ASP.NET MVC 4中的控制器

[英]How to get checkbox selected value and pass to controller in asp.net mvc 4

I am trying to get the selected checkboxes value 我正在尝试获取选定的复选框值

this are my models, 这是我的模特

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; }
}

here's my razor view 这是我的剃刀视图

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

and here's my controller 这是我的控制器

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

I know I need to pass the selected check boxes value to a string using javascript and pass the string to a controller. 我知道我需要使用javascript将选定的复选框值传递给字符串,并将该字符串传递给控制器​​。 But i have no idea how to do it since I'm a newbie in javascript and MVC. 但是我不知道该怎么做,因为我是javascript和MVC的新手。

Based on the comments that an ajax post is not required, your AssignProductsViewModel requires an additional property to bind the checkboxes 根据不需要ajax帖子的评论,您的AssignProductsViewModel需要附加属性来绑定复选框

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

In the view, use a for loop or a custom EditorTemplate to render the collection do the controls are correctly named with indexers. 在视图中,使用for循环或自定义EditorTemplate渲染集合,以确保使用索引器正确命名控件。 A foreach loop generates duplicate id (invalid html) and name attributes (cannot be bound to a collection) 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)
  }
  ....
}

and post back to 然后发回

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

Alternatively, you can use an EditorTemplate for type of AssignProductsViewModel to render the collection 另外,您可以使用EditorTemplate作为AssignProductsViewModel类型来呈现集合

In /Views/Shared/EditorTemplates/AssignProductsViewModel.cshtml /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)

and in the main view 在主视图中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中将DropdownList Selected值与JavaScript传递给Asp.net MVC 4中的Controller? - How can I pass DropdownList Selected value with JavaScript to Controller in Asp.net MVC 4? 如何获取DropDownList选定的值,并将其发送到ASP.NET MVC 4中的控制器并使用JavaScript? - How can I get a DropDownList selected value and send it to the controller in ASP.NET MVC 4 and using JavaScript? 如何使用Jquery从Asp.net MVC中的CheckBox中获取选定的值? - How to get the selected values from CheckBox in Asp.net MVC using Jquery? 如何从ASP.NET MVC的多个分页中获取所有选中的复选框? - How to get all selected checkbox from multiple pagination page in asp.net mvc? 如何将文本框的值从锚标记传递到ASP.NET MVC中的控制器方法 - How to pass value of textbox from anchor tag to controller method in asp.net mvc 单击asp.net MVC中的按钮,如何将textarea值传递给控制器​​操作? - How to pass textarea value to controller action on the click of a button in asp.net mvc? 如何将JavaScript数据传递到控制器ASP.NET MVC - How to pass javascript data to controller ASP.NET MVC 将字典传递给 controller asp.net mvc - pass dictionary to controller asp.net mvc ASP.NET MVC通过在Controller中使用ID获取div的值 - ASP.NET MVC Get value of div by using ID in Controller 如何传递dropdownlist选定的值以获取MVC控制器中的功能 - how to pass dropdownlist selected value to get function in controller in mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM