MVC3.0中的CheckboxList

如何在asp.net MVC中创建一个checkboxList,然后使用checkboxList处理事件



POPMUISE
浏览 742回答 2
2回答

鸿蒙传说

您可能有一个视图模型:public class MyViewModel{&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public bool IsChecked { get; set; }}控制器:public class HomeController : Controller{&nbsp; &nbsp; public ActionResult Index()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var model = new[]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new MyViewModel { Id = 1, IsChecked = false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new MyViewModel { Id = 2, IsChecked = true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new MyViewModel { Id = 3, IsChecked = false },&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; return View(model);&nbsp; &nbsp; }&nbsp; &nbsp; [HttpPost]&nbsp; &nbsp; public ActionResult Index(IEnumerable<MyViewModel> model)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // TODO: Handle the user selection here&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }}视图(~/Views/Home/Index.cshtml):@model IEnumerable<AppName.Models.MyViewModel>@{&nbsp; &nbsp; ViewBag.Title = "Home Page";}@using (Html.BeginForm()){&nbsp; &nbsp; @Html.EditorForModel()&nbsp; &nbsp; <input type="submit" value="OK" />}以及相应的编辑器模板(~/Views/Home/EditorTemplates/MyViewModel.cshtml):@model AppName.Models.MyViewModel@Html.HiddenFor(x => x.Id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Html.CheckBoxFor(x => x.IsChecked)现在,当您提交表单时,您将获得一个值列表,并且将为每个值选择是否选中。
打开App,查看更多内容
随时随地看视频慕课网APP