網站首頁 編程語言 正文
在上一篇文章的最后,列出了一些常見的HtmlHelper的方法,這些都是ASP.NET MVC已經定義好的,如果我們想自己定義一個HtmlHelper方法可以嗎?答案是肯定的,那么如何自定義一個HtmlHelper方法呢?
以Label()方法為例,查看Label方法的定義:
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionaryhtmlAttributes = 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 { ////// HTML的擴展類 /// public static class HtmlHelperExt { ////// 用C#代碼自定義一個span標簽的擴展方法 /// /// /// /// /// /// ///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"; }ASP.NET
ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.
Learn more »
@Html.Messager("lblMessage", "lblMessage", "color:red;font-weight:bold;", "自定義span標簽")
Getting started
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.
Learn more »
Get more libraries
NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.
Learn more »
Web Hosting
You can easily find a web hosting company that offers the right mix of features and price for your applications.
Learn more »
運行結果如下:
原文鏈接:https://www.cnblogs.com/dotnet261010/p/8900172.html
相關推薦
- 2023-01-13 基于C#實現屏幕取色器_C#教程
- 2022-11-24 詳解如何實現一個Kotlin函數類型_Android
- 2022-07-18 python中數組array和列表list的基本用法及區別解析_python
- 2022-03-29 詳解python字符串相關str_python
- 2022-03-14 -bash: 未預期的符號 `(‘ 附近有語法錯誤的解決辦法
- 2022-11-18 Go與Redis實現分布式互斥鎖和紅鎖_Golang
- 2022-04-17 You need tcl 8.5 or newer in order to run the Redi
- 2022-07-26 Android自定義評分控件的完整實例_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支