ASP.NET MVC擴展HtmlHelper方法
在上一篇文章的最後,列出瞭一些常見的HtmlHelper的方法,這些都是ASP.NET MVC已經定義好的,如果我們想自己定義一個HtmlHelper方法可以嗎?答案是肯定的,那麼如何自定義一個HtmlHelper方法呢?
以Label()方法為例,查看Label方法的定義:
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionary<string, object> htmlAttributes = null) { string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (String.IsNullOrEmpty(resolvedLabelText)) { return MvcHtmlString.Empty; } TagBuilder tag = new TagBuilder("label"); tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName))); tag.SetInnerText(resolvedLabelText); tag.MergeAttributes(htmlAttributes, replaceExisting: true); return tag.ToMvcHtmlString(TagRenderMode.Normal); }
這是MVC的源碼中對Label()擴展方法的定義,我們可以參考MVC中源碼定義擴展方法的方式自定義一個擴展方法。
下面以span標簽為例進行擴展,擴展方法定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcHtmlHelper.Helper { /// <summary> /// HTML的擴展類 /// </summary> public static class HtmlHelperExt { /// <summary> /// 用C#代碼自定義一個span標簽的擴展方法 /// </summary> /// <param name="htlper"></param> /// <param name="id"></param> /// <param name="name"></param> /// <param name="style"></param> /// <param name="message"></param> /// <returns></returns> public static MvcHtmlString Messager(this HtmlHelper htlper, string id,string name, string style, object message) { if (message != null) { TagBuilder builder = new TagBuilder("span"); builder.MergeAttribute("style", style); //定義樣式 builder.MergeAttribute("id", id); // 定義Id builder.MergeAttribute("name", name); // 定義name builder.SetInnerText(message.ToString()); //ToMvcHtmlString是在TagBuilderExtensions擴展類中定義的 return builder.ToMvcHtmlString(TagRenderMode.Normal); } return MvcHtmlString.Empty; } } }
TagBuilderExtensions擴展方法定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcHtmlHelper.Helper { public static class TagBuilderExtensions { public static MvcHtmlString ToMvcHtmlString(this TagBuilder tagBuilder, TagRenderMode renderMode) { System.Diagnostics.Debug.Assert(tagBuilder != null); return new MvcHtmlString(tagBuilder.ToString(renderMode)); } } }
視圖頁面代碼如下:
@using MvcHtmlHelper.Helper; @{ ViewBag.Title = "Home Page"; } <div class="jumbotron"> <h1>ASP.NET</h1> <p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p> <p><a href="https://asp.net" rel="external nofollow" class="btn btn-primary btn-lg">Learn more »</a></p> <p> <!--使用自定義的Messager方法--> @Html.Messager("lblMessage", "lblMessage", "color:red;font-weight:bold;", "自定義span標簽") </p> </div> <div class="row"> <div class="col-md-4"> <h2>Getting started</h2> <p> ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that enables a clean separation of concerns and gives you full control over markup for enjoyable, agile development. </p> <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301865" rel="external nofollow" >Learn more »</a></p> </div> <div class="col-md-4"> <h2>Get more libraries</h2> <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p> <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301866" rel="external nofollow" >Learn more »</a></p> </div> <div class="col-md-4"> <h2>Web Hosting</h2> <p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p> <p><a class="btn btn-default" href="https://go.microsoft.com/fwlink/?LinkId=301867" rel="external nofollow" >Learn more »</a></p> </div> </div>
運行結果如下:
到此這篇關於ASP.NET MVC擴展HtmlHelper方法的文章就介紹到這瞭。希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- ASP.NET MVC使用jQuery Template實現批量更新
- Unity實現紅酒識別的示例代碼
- Unity實現動物識別的示例代碼
- Unity實現菜品識別的示例代碼
- Unity接入百度AI實現通用物體和場景識別