我实施MVC复选框列表,虽然我成功得到期望的结果,我在方法有一个疑问:
I'm implementing check box list in MVC, although I succeed to get the desired result, I have a doubt in my approach:
public class AdditionalServicesModel { public IList<SelectListItem> AdditionalServices { get; set; } }=============================================== ==========================
=========================================================================
public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { AdditionalServicesModel objAdditionalServicesModel = new AdditionalServicesModel(); List<SelectListItem> services = new List<SelectListItem>(); services.Add(new SelectListItem { Text = "service-1", Value = "1", Selected=false }); services.Add(new SelectListItem { Text = "service-2", Value = "2", Selected=false }); services.Add(new SelectListItem { Text = "service-3", Value = "3", Selected=false }); objAdditionalServicesModel.AdditionalServices = services; return View(objAdditionalServicesModel); } [HttpPost] public ActionResult Index(AdditionalServicesModel result) { return RedirectToAction("Thanks", "Home"); } public ActionResult Thanks() { return View(); } }=============================================== ==========================
=========================================================================
@model checkboxes.Models.AdditionalServicesModel @{ ViewBag.Title = "Index"; Layout = null; } @using (Html.BeginForm("Index", "Home", FormMethod.Post)) { for (int i = 0; i < Model.AdditionalServices.Count; i++) { <ul> <li> @Html.CheckBoxFor(m=>Model.AdditionalServices[i].Selected, new { id = "Chk-" + i}) @Html.Label(Model.AdditionalServices[i].Text, new { @for = "Chk-" + i }) @Html.HiddenFor(m => Model.AdditionalServices[i].Text) @Html.HiddenFor(m=> Model.AdditionalServices[i].Value) </li> </ul> } <input type="submit" value="POST to Controller" /> }1)对于一个复选框我应该创建2个额外的隐藏字段。有没有更好的方法吗?我觉得喜欢它只是不对的这一切很长的路要走只是为了送复选框值+名,这将是更容易使用JavaScript来收集它的价值和通过JSON发送,但我不会有一个不显眼的验证...
1) For one checkbox I should create 2 additional hidden fields. Is there a better approach? I feel like it just wrong to make all this long way just to send checkboxes values + names, It would be much easier to collect its values with javascript and send it via Json, but then I will not have an unobtrusive validation...
2)我把所有的复选框,而我需要发送唯一入选的盒子。有没有一种方法与形式做后期?
2) I send all check boxes while I need to send only selected boxes. Is there a way to do it with forms post?
推荐答案创建一个自定义服务复选框项级(可重复使用),并进行了列表或枚举它们作为你AdditionalServicesModel的属性。此外,它可能是一个更好的主意,以创建模型的构造函数,因此您不必分配控制器内部的模型属性。
Create a custom 'services check box item' class (reusable), and make a list or enumerable of them as a property in your AdditionalServicesModel. Also, its probably a better idea to create a constructor of the model so you don't have to assign the model properties inside the controller.
public class ServicesItem { public bool Selected { get; set; } public string Value { get; set; } public string Text { get; set; } } public class AdditionalServicesModel { public AdditionalServicesModel(IList<ServicesItem> items){ this.AdditionalServices = items; } public IList<ServicesItem> AdditionalServices { get; set; } }创建附加服务自定义编辑模板,可以轻松地在您的视图中引用(你不必添加一个隐藏的文本,因为只有价值和选择的属性将被默认绑定回到模型:
Create a custom editor template for Additional Services, to easily reference in your view (you don't have to add a hidden for the text, as only the value and selected properties will be default binded back to the model:
@Html.CheckBoxFor(m => Selected) @Html.LabelFor(m => Text) @Html.HiddenFor(m => Value)然后弹出编辑模板到您的视图(让MVC做它的魔力 - 看看标记,看看它做什么):
Then pop the editor template into your view (let MVC do its magic - have a look at the markup to see what it does):
@Html.EditorFor(m => m.AdditionalServices)然后在你的控制器检查自动绑定值:
Then inspect the automatically bound values in your controller:
public class HomeController : Controller { public ActionResult Index() { List<SelectListItem> services = new List<SelectListItem>(); services.Add(new SelectListItem { Text = "service-1", Value = "1", Selected=false }); services.Add(new SelectListItem { Text = "service-2", Value = "2", Selected=false }); services.Add(new SelectListItem { Text = "service-3", Value = "3", Selected=false }); return View(new AdditionalServicesModel(services)); } [HttpPost] public ActionResult Index(AdditionalServicesModel result) { var selectedServicesList = result.AdditionalServices.Where(s => s.Selected); return RedirectToAction("Thanks", "Home"); } }更多推荐
Asp.Net的mvc复选框列表
发布评论