asp.net-mvc – 如何使用带有IEnumerable的Html.CheckBox(list)和验证
发布时间:2020-09-01 22:46:03 所属栏目:asp.Net 来源:互联网
导读:我正在一个页面上,用户需要填写一些信息,最后选择一个或多个带有复选框的客户. 客户列表是IEnumerable Customer我传入我的模型.我如何使用.CheckBoxFor()创建复选框列表? 最后,我希望能够验证是否已选择至少1个复选框. 请求是保存用户输入的信息的对象. % fo
我正在一个页面上,用户需要填写一些信息,最后选择一个或多个带有复选框的客户. 客户列表是IEnumerable< Customer>我传入我的模型.我如何使用.CheckBoxFor()创建复选框列表? 最后,我希望能够验证是否已选择至少1个复选框. 请求是保存用户输入的信息的对象. <% foreach (var customer in Model.Request.Customers) { %> <%= Html.CheckBoxFor(/* customer */) %> <% } %> 谁能指出我正确的方向?或者我这样做是错的? 解决方法您可以创建一个自定义html扩展类并重载CheckBoxFor方法,如下所示.该方法将metadata.Model计算为传递给它的值(如美国州).您可以从ControllerAction中的FormCollection获取复选框值:public ActionResult Edit(FormCollection formCollection) { // Get the value(s) string checkBox = formCollection["State"]; // perform validation .... } 示例假定keyvaluepair通用列表 <% foreach (var element in UnitedStatesDictionary()) { %> <%= Html.CheckBoxFor(model => model.State,null,element.Key) %><%= element.Value %><br /> <% } %> HtmlExtensions.cs using System; using System.Linq; using System.Linq.Expressions; using System.Web.Mvc; using System.Web.Routing; public static class HtmlExtensions { /// <summary> /// Checks the box for. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="html">The HTML.</param> /// <param name="expression">The expression.</param> /// <returns>Checkbox</returns> public static MvcHtmlString CheckBoxFor<TModel,TValue>(this HtmlHelper<TModel> html,Expression<Func<TModel,TValue>> expression) { return CheckBoxFor(html,expression,new RouteDirection()); } /// <summary> /// Checks the box for. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="html">The HTML.</param> /// <param name="expression">The expression.</param> /// <param name="htmlAttributes">The HTML attributes.</param> /// <returns>Checkbox</returns> public static MvcHtmlString CheckBoxFor<TModel,TValue>> expression,object htmlAttributes) { return CheckBoxFor(html,htmlAttributes,""); } /// <summary> /// Checks the box for. /// </summary> /// <typeparam name="TModel">The type of the model.</typeparam> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="html">The HTML.</param> /// <param name="expression">The expression.</param> /// <param name="htmlAttributes">The HTML attributes.</param> /// <param name="checkedValue">The checked value.</param> /// <returns>Checkbox</returns> public static MvcHtmlString CheckBoxFor<TModel,object htmlAttributes,string checkedValue) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression,html.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); TagBuilder tag = new TagBuilder("input"); tag.Attributes.Add("type","checkbox"); tag.Attributes.Add("name",metadata.PropertyName); if (!string.IsNullOrEmpty(checkedValue)) { tag.Attributes.Add("value",checkedValue); } else { tag.Attributes.Add("value",metadata.Model.ToString()); } if (htmlAttributes != null) { tag.MergeAttributes(new RouteValueDictionary(htmlAttributes)); } if (metadata.Model.ToString() == checkedValue) { tag.Attributes.Add("checked","checked"); } return MvcHtmlString.Create(tag.ToString(TagRenderMode.SelfClosing)); } } 我在这里,这是我的美国名单,以完成代码: /// <summary> /// United States dictionary. /// </summary> /// <returns>List of United States</returns> public static List<KeyValuePair<string,string>> UnitedStatesDictionary() { var arrList = new List<KeyValuePair<string,string>>(); arrList.Add(new KeyValuePair<string,string>("AL","Alabama")); arrList.Add(new KeyValuePair<string,string>("AK","Alaska")); arrList.Add(new KeyValuePair<string,string>("AZ","Arizona" )); arrList.Add(new KeyValuePair<string,string>("AR","Arkansas" )); arrList.Add(new KeyValuePair<string,string>("CA","California" )); arrList.Add(new KeyValuePair<string,string>("CO","Colorado" )); arrList.Add(new KeyValuePair<string,string>("CT","Connecticut" )); arrList.Add(new KeyValuePair<string,string>("DE","Delaware" )); arrList.Add(new KeyValuePair<string,string>("DC","District Of Columbia" )); arrList.Add(new KeyValuePair<string,string>("FL","Florida" )); arrList.Add(new KeyValuePair<string,string>("GA","Georgia" )); arrList.Add(new KeyValuePair<string,string>("HI","Hawaii" )); arrList.Add(new KeyValuePair<string,string>("ID","Idaho" )); arrList.Add(new KeyValuePair<string,string>("IL","Illinois" )); arrList.Add(new KeyValuePair<string,string>("IN","Indiana" )); arrList.Add(new KeyValuePair<string,string>("IA","Iowa" )); arrList.Add(new KeyValuePair<string,string>("KS","Kansas" )); arrList.Add(new KeyValuePair<string,string>("KY","Kentucky" )); arrList.Add(new KeyValuePair<string,string>("LA","Louisiana" )); arrList.Add(new KeyValuePair<string,string>("ME","Maine" )); arrList.Add(new KeyValuePair<string,string>("MD","Maryland" )); arrList.Add(new KeyValuePair<string,string>("MA","Massachusetts" )); arrList.Add(new KeyValuePair<string,string>("MI","Michigan" )); arrList.Add(new KeyValuePair<string,string>("MN","Minnesota" )); arrList.Add(new KeyValuePair<string,string>("MS","Mississippi" )); arrList.Add(new KeyValuePair<string,string>("MO","Missouri" )); arrList.Add(new KeyValuePair<string,string>("MT","Montana" )); arrList.Add(new KeyValuePair<string,string>("NE","Nebraska" )); arrList.Add(new KeyValuePair<string,string>("NV","Nevada" )); arrList.Add(new KeyValuePair<string,string>("NH","New Hampshire" )); arrList.Add(new KeyValuePair<string,string>("NJ","New Jersey" )); arrList.Add(new KeyValuePair<string,string>("NM","New Mexico" )); arrList.Add(new KeyValuePair<string,string>("NY","New York" )); arrList.Add(new KeyValuePair<string,string>("NC","North Carolina" )); arrList.Add(new KeyValuePair<string,string>("ND","North Dakota" )); arrList.Add(new KeyValuePair<string,string>("OH","Ohio" )); arrList.Add(new KeyValuePair<string,string>("OK","Oklahoma" )); arrList.Add(new KeyValuePair<string,string>("OR","Oregon" )); arrList.Add(new KeyValuePair<string,string>("PA","Pennsylvania" )); arrList.Add(new KeyValuePair<string,string>("RI","Rhode Island" )); arrList.Add(new KeyValuePair<string,string>("SC","South Carolina" )); arrList.Add(new KeyValuePair<string,string>("SD","South Dakota" )); arrList.Add(new KeyValuePair<string,string>("TN","Tennessee" )); arrList.Add(new KeyValuePair<string,string>("TX","Texas" )); arrList.Add(new KeyValuePair<string,string>("UT","Utah" )); arrList.Add(new KeyValuePair<string,string>("VT","Vermont" )); arrList.Add(new KeyValuePair<string,string>("VA","Virginia" )); arrList.Add(new KeyValuePair<string,string>("WA","Washington" )); arrList.Add(new KeyValuePair<string,string>("WV","West Virginia" )); arrList.Add(new KeyValuePair<string,string>("WI","Wisconsin" )); arrList.Add(new KeyValuePair<string,string>("WY","Wyoming" )); return arrList; } (编辑:台州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 为一个MVC视图使用两个强类型模型
- asp.net-mvc – 我可以获取html.HiddenFor / Html.Hidden创
- asp.net – 在Azure网站上启用gzip压缩
- asp.net-mvc – Ajax重定向到页面而不是更新目标
- asp.net编程实现删除文件夹及文件夹下文件的方法
- asp.net-mvc – 无法在Web服务器上启动调试. Web服务器找不
- asp.net – 使用AJAX进行WCF调用
- 优化 – 字典/客户端VS应用程序变量
- 从app_data中删除文件夹时如何防止asp.net重新编译?
- ASP.NET 程序中删除文件夹导致session失效问题的解决办法分
推荐文章
站长推荐
- .net – IIS 6.0和ASPX中的404自定义错误不起作用
- asp.net-mvc – Url.Action生成查询字符串,以任何
- asp.net – 如何在C#2.0中的Web.config中加密用户
- asp.net-mvc-4 – AngularJs,DropZone.Js,MVC4 –
- asp.net-mvc – ASP.Net [HiddenInput]数据属性在
- 如何在服务器端缓存ASP.NET自定义HttpHandler响应
- asp.net-mvc – 为什么在我的ASP MVC4应用程序中
- asp.net-mvc-3 – 如何关闭我的整个ASP.NET MVC
- asp.net-mvc – 从Api控制器内生成绝对的url to
- asp.net – Oracle.ManagedDataAccess:TNS:无法
热点阅读