[英]Checkbox collection in strongly typed view
I use MVC 4 and I have the below model defined and use the same in View. 我使用MVC 4,并定义了以下模型,并在View中使用了该模型。
I want to show item in a view and render checkbox for each city. 我想在视图中显示项目并为每个城市渲染复选框。 So user can select multiple city for a perticular item.
因此,用户可以为一个垂直项目选择多个城市。
public class City
{
int id{get;set;}
string name{get;set;}
}
public class Item
{
int id{get;set;}
List<City> cities{get;set;}
}
public class ItemController{
public ActionResult Save(Item item){
List<city> selectedCitirs=item.cities; // here null
return View();
}
public ActionResult Get(int id){
Item item=Service.GetItem(id);
return View(item);
}
}
@model item
@foreach(City c in item.cities){
@Html.CheckBoxFor(c=>c.name)
}
In a controller i would like to get the selected cities name and want to store in a DB like city1,city2,city3 format.
在控制器中,我想获取选定的城市名称,并希望以类似于city1,city2,city3格式的数据库进行存储。
But controller not giving item.cities. 但是控制器没有给出item.cities。 How to do?
怎么做?
Try 尝试
@model Item
@for (int i=0; i < Model.cities.Length; ++i)
{
@Html.CheckBoxFor(m => m.cities[i].name)
}
See also: 也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.