Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Customize a HtmlHelper method in ASP.NET MVC

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "a custom HtmlHelper method in ASP.NET MVC", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "Custom a HtmlHelper method in ASP.NET MVC".

Taking the Label () method as an example, look at the definition of the Label method:

Internal static MvcHtmlString LabelHelper (HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null, IDictionary 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);}

This is the definition of the Label () extension method in the source code of MVC. We can customize an extension method by referring to the way the source code defines the extension method in MVC.

The following extension takes the span tag as an example. The extension method is defined as follows:

Using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc The extension class of namespace MvcHtmlHelper.Helper {/ HTML / public static class HtmlHelperExt {/ customize the extension method of a span tag with C # code / / 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); / define style builder.MergeAttribute ("id", id); / / define Id builder.MergeAttribute ("name", name); / / define name builder.SetInnerText (message.ToString ()) / / ToMvcHtmlString is return builder.ToMvcHtmlString (TagRenderMode.Normal) defined in the TagBuilderExtensions extension class;} return MvcHtmlString.Empty;}

The TagBuilderExtensions extension method is defined as follows:

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));}

The view page code is as follows:

@ 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;", "Custom span tag")

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 »

The running results are as follows:

The above is all the content of the article "customizing a HtmlHelper method in ASP.NET MVC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report