Asp.Net的mvc复选框列表

编程入门 行业动态 更新时间:2024-10-26 20:27:58
本文介绍了Asp.Net的mvc复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我实施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复选框列表

本文发布于:2023-10-12 01:35:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   列表   Asp   Net   mvc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!