ASP.NET MVC擴(kuò)展帶驗(yàn)證的單選按鈕
在ASP.NET MVC4中,HtmlHelper為我們提供了Html.RadioButton()方法用來(lái)顯示Radio Button單選按鈕。如果想顯示一組單選按鈕,通常的做法是遍歷一個(gè)集合把每個(gè)單選按鈕顯示出來(lái)。本篇嘗試寫一個(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類的集合將作為所有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 citiesselect 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)鏈接
相關(guān)文章:
1. ASP.NET MVC使用異步Action的方法2. ASP.Net MVC利用NPOI導(dǎo)入導(dǎo)出Excel的示例代碼3. 使用EF Code First搭建簡(jiǎn)易ASP.NET MVC網(wǎng)站并允許數(shù)據(jù)庫(kù)遷移4. ASP.NET MVC實(shí)現(xiàn)城市或車型三級(jí)聯(lián)動(dòng)5. ASP.NET MVC通過(guò)勾選checkbox更改select的內(nèi)容6. ASP.NET MVC使用Session會(huì)話保持表單狀態(tài)7. ASP.NET MVC獲取多級(jí)類別組合下的產(chǎn)品8. ASP.NET MVC使用typeahead.js實(shí)現(xiàn)輸入智能提示功能9. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息10. ASP.NET MVC增加一條記錄同時(shí)添加N條集合屬性所對(duì)應(yīng)的個(gè)體
