ASP.NET?MVC擴(kuò)展帶驗(yàn)證的單選按鈕
在ASP.NET MVC4中,HtmlHelper為我們提供了Html.RadioButton()方法用來(lái)顯示Radio Button單選按鈕。如果想顯示一組單選按鈕,通常的做法是遍歷一個(gè)集合把每個(gè)單選按鈕顯示出來(lái)。本篇嘗試寫(xiě)一個(gè)擴(kuò)展方法用來(lái)展示一組帶驗(yàn)證的單選按鈕。
首先來(lái)擴(kuò)展HtmlHelper,擴(kuò)展方法中接收一個(gè)SelectListItem的集合,遍歷這個(gè)集合把每個(gè)單選按鈕顯示出來(lái),并且讓這些單選按鈕具有不同的id屬性值。
using System.Collections.Generic; using System.Linq.Expressions; using System.Text; using System.Web.Mvc.Html; namespace System.Web.Mvc { public static class HtmlExtensions { public static MvcHtmlString RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> list) { //獲取元數(shù)據(jù) var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); var sb = new StringBuilder(); if (list != null) { foreach (var item in list) { //把屬性名和集合元素的Value值拼接作為元素的id var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value); //創(chuàng)建單選按鈕 var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text)); var radio = htmlHelper.RadioButtonFor(expression, item.Value, new {id = id}).ToHtmlString(); sb.AppendFormat("<div class=\"RadioButton\">{0}{1}</div>", radio, label); } } return MvcHtmlString.Create(sb.ToString()); } } }
假設(shè),現(xiàn)在有一個(gè)View Model,其中的一個(gè)屬性要求必須。
using System.ComponentModel.DataAnnotations; namespace MvcApplication1.Models { public class Vm { [Required(ErrorMessage = "必填")] public int CityId { get; set; } } }
以下City類(lèi)的集合將作為所有Radio Button的數(shù)據(jù)源。
namespace MvcApplication1.Models { public class City { public int Id { get; set; } public string Name { get; set; } } }
在HomeController中,提供一個(gè)Action方法啊,把City的集合轉(zhuǎn)換成SelectListItem集合傳遞給視圖。
using System.Collections.Generic; using System.Linq; using System.Web.Mvc; using MvcApplication1.Models; namespace MvcApplication1.Controllers { public class HomeController : Controller { public ActionResult Index() { List<City> cities = new List<City>() { new City(){Id = 1, Name = "青島"}, new City(){Id = 2, Name = "濟(jì)南"}, new City(){Id = 3, Name = "平度"} }; ViewData["c"] = from c in cities select new SelectListItem() {Text = c.Name, Value = c.Id.ToString()}; return View(new Vm()); } [HttpPost] public ActionResult Index(Vm vm) { if (ModelState.IsValid) { return Content(vm.CityId.ToString()); } else { return View(vm); } } } }
在_Layout.csthml中,必須具備客戶端驗(yàn)證js。
<head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.Title</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/jqueryval") </head> <body> @RenderBody() @RenderSection("scripts", required: false) </body>
在Home/Index.chtml中,使用擴(kuò)展方法顯示Radio Button組。
@model MvcApplication1.Models.Vm @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <style type="text/css"> .RadioButton { float:left; } </style> @using (Html.BeginForm("Index", "Home", FormMethod.Post, new {id = "addForm"})) { @Html.RadioButtonListFor(v => v.CityId, ViewData["c"] as IEnumerable<SelectListItem>) @Html.ValidationMessageFor(v => v.CityId) <input type="submit" value="提交"/> }
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移
- ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車(chē)
- ASP.NET MVC獲取多級(jí)類(lèi)別組合下的產(chǎn)品
- ASP.NET MVC使用正則表達(dá)式驗(yàn)證手機(jī)號(hào)碼
- ASP.NET?MVC實(shí)現(xiàn)登錄后跳轉(zhuǎn)到原界面
- ASP.NET?MVC使用jQuery的Load方法加載靜態(tài)頁(yè)面及注意事項(xiàng)
- ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法
- ASP.NET?MVC使用JSAjaxFileUploader插件實(shí)現(xiàn)單文件上傳
- ASP.NET?MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)
相關(guān)文章
.NET使用報(bào)表工具FastReport實(shí)現(xiàn)打印功能
這篇文章介紹了.NET使用報(bào)表工具FastReport實(shí)現(xiàn)打印功能的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03.NET(C#)連接各類(lèi)數(shù)據(jù)庫(kù)代碼-集錦
.NET(C#)連接各類(lèi)數(shù)據(jù)庫(kù)代碼-集錦...2007-03-03ASP.NET中GridView、DataList、DataGrid三個(gè)數(shù)據(jù)控件foreach遍歷用法示例
這篇文章主要介紹了ASP.NET中GridView、DataList、DataGrid三個(gè)數(shù)據(jù)控件foreach遍歷用法,結(jié)合實(shí)例形式分析了GridView、DataList、DataGrid使用foreach及for語(yǔ)句進(jìn)行數(shù)據(jù)遍歷的具體使用方法,需要的朋友可以參考下2016-08-08ASP.NET(C#)驗(yàn)證數(shù)字的兩種方法
ASP.NET(C#)驗(yàn)證數(shù)字的兩種方法,需要的朋友可以參考一下2013-06-06.Net Web Api中利用FluentValidate進(jìn)行參數(shù)驗(yàn)證的方法
最近在做Web API,用到了流式驗(yàn)證,就簡(jiǎn)單的說(shuō)說(shuō)這個(gè)流式驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于.Net Web Api中利用FluentValidate進(jìn)行參數(shù)驗(yàn)證的相關(guān)資料,,需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07aspx實(shí)現(xiàn)的 jquery ui 的 flexgrid demo
這幾天沒(méi)事研究著jquery,真是個(gè)好東西,慢慢的知道了有jquery ui,一開(kāi)始就被華麗的界面和簡(jiǎn)單的操作給吸引了,尤其是里面的flexgrid,對(duì)我而言可以說(shuō)是非常寶貴的東西2009-12-12.Net判斷一個(gè)對(duì)象是否為數(shù)值類(lèi)型實(shí)例
這篇文章主要介紹了.Net判斷一個(gè)對(duì)象是否為數(shù)值類(lèi)型的方法,實(shí)例講述了一個(gè)國(guó)外的示例并對(duì)其進(jìn)行了改進(jìn),非常實(shí)用,需要的朋友可以參考下2014-10-10