Asp.net mvc 如何获取复选框列表的选定值?
如果我在表单中有一个复选框列表,像这样Asp.net mvc 如何获取复选框列表的选定值?,asp.net-mvc,checkboxlist,Asp.net Mvc,Checkboxlist,如果我在表单中有一个复选框列表,像这样 @using (Html.BeginForm("Save", "Product", FormMethod.Post)) { ... @Html.CheckBoxList("Categories", Model.AllCategories); ... } 。。。如何在控制器操作中获取选定值(选中值)的列表 例如,如果复选框列表中的项的值为: 猫。一, 猫。二, 猫。三,
@using (Html.BeginForm("Save", "Product", FormMethod.Post))
{
...
@Html.CheckBoxList("Categories", Model.AllCategories);
...
}
。。。如何在控制器操作中获取选定值(选中值)的列表
例如,如果复选框列表中的项的值为:
猫。一,
猫。二,
猫。三,
猫。四,
…和
Cat。2
和Cat。如果选择了3,如何获取包含这些值的数组?在最简单的情况下,控制器操作应如下所示(在产品控制器中):
在更复杂的情况下(具有更多表单字段),最好使用视图模型,并向其添加“类别”属性
public class MyViewModel
{
...
public string[] Categories { get; set; }
...
}
控制器操作:
[HttpPost]
public ActionResult Save(MyViewModel model)
{
// Process selected checkbox values here, using the model.Categories array
...
}
简单的问答,但希望它能帮助寻找答案的人(就像我第一次开始学习ASP.NETMVC时一样)
另外,如果你有更好或更详细的信息,请发布。查看这个问题的答案,我一直在使用这个,而且效果很好
[HttpPost]
public ActionResult Save(MyViewModel model)
{
// Process selected checkbox values here, using the model.Categories array
...
}