MVC3.0 中的复选框列表 [英] CheckboxList in MVC3.0
本文介绍了MVC3.0 中的复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 asp.net MVC 中创建 checkboxList,然后使用 checkboxList 处理事件
How can I create a checkboxList in asp.net MVC and then to handle the event with the checkboxList
推荐答案
你可以有一个视图模型:
You could have a view model:
public class MyViewModel
{
public int Id { get; set; }
public bool IsChecked { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new[]
{
new MyViewModel { Id = 1, IsChecked = false },
new MyViewModel { Id = 2, IsChecked = true },
new MyViewModel { Id = 3, IsChecked = false },
};
return View(model);
}
[HttpPost]
public ActionResult Index(IEnumerable<MyViewModel> model)
{
// TODO: Handle the user selection here
...
}
}
一个视图(~/Views/Home/Index.cshtml
):
@model IEnumerable<AppName.Models.MyViewModel>
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
@Html.EditorForModel()
<input type="submit" value="OK" />
}
和相应的编辑器模板(~/Views/Home/EditorTemplates/MyViewModel.cshtml
):
and the corresponding Editor template (~/Views/Home/EditorTemplates/MyViewModel.cshtml
):
@model AppName.Models.MyViewModel
@Html.HiddenFor(x => x.Id)
@Html.CheckBoxFor(x => x.IsChecked)
现在,当您提交表单时,您将获得一个值列表以及每个值是否被选中.
Now when you submit the form you would get a list of values and for each value whether it is checked or not.
这篇关于MVC3.0 中的复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文