[英]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.