最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET MVC擴(kuò)展HtmlHelper方法

 更新時(shí)間:2022年03月08日 11:54:50   作者:.NET開發(fā)菜鳥  
這篇文章介紹了ASP.NET MVC擴(kuò)展HtmlHelper的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

上一篇文章的最后,列出了一些常見的HtmlHelper的方法,這些都是ASP.NET MVC已經(jīng)定義好的,如果我們想自己定義一個(gè)HtmlHelper方法可以嗎?答案是肯定的,那么如何自定義一個(gè)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的源碼中對(duì)Label()擴(kuò)展方法的定義,我們可以參考MVC中源碼定義擴(kuò)展方法的方式自定義一個(gè)擴(kuò)展方法。

下面以span標(biāo)簽為例進(jìn)行擴(kuò)展,擴(kuò)展方法定義如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcHtmlHelper.Helper
{
    /// <summary>
    /// HTML的擴(kuò)展類
    /// </summary>
    public static class HtmlHelperExt
    {
        /// <summary>
        /// 用C#代碼自定義一個(gè)span標(biāo)簽的擴(kuò)展方法
        /// </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擴(kuò)展類中定義的
                return builder.ToMvcHtmlString(TagRenderMode.Normal);
            }
            return MvcHtmlString.Empty;
        }
    }
}

TagBuilderExtensions擴(kuò)展方法定義如下:

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  rel="external nofollow"  class="btn btn-primary btn-lg">Learn more &raquo;</a></p>
    <p>
        <!--使用自定義的Messager方法-->
        @Html.Messager("lblMessage", "lblMessage", "color:red;font-weight:bold;", "自定義span標(biāo)簽")
    </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"  rel="external nofollow" >Learn more &raquo;</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"  rel="external nofollow" >Learn more &raquo;</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"  rel="external nofollow" >Learn more &raquo;</a></p>
    </div>
</div>

運(yùn)行結(jié)果如下:

到此這篇關(guān)于ASP.NET MVC擴(kuò)展HtmlHelper方法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • .net core整合log4net的解決方案

    .net core整合log4net的解決方案

    這篇文章主要給大家介紹了關(guān)于.net core整合log4net的解決方案,文中通過圖文介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Asp.Net Mvc2 OA 工作流設(shè)計(jì)思路[圖]

    Asp.Net Mvc2 OA 工作流設(shè)計(jì)思路[圖]

    回老家上班的新公司,第一個(gè)項(xiàng)目:OA。以前沒有做過OA,因?yàn)樵O(shè)計(jì)到工作流這一塊的東西,所以自己去進(jìn)行了相關(guān)的了解,于是有了這篇博客(以下文字只是個(gè)人理解,高手漂過)
    2012-10-10
  • 詳解.Net core2.0日志組件Log4net、Nlog簡(jiǎn)單性能測(cè)試

    詳解.Net core2.0日志組件Log4net、Nlog簡(jiǎn)單性能測(cè)試

    這篇文章主要介紹了詳解.Net core2.0日志組件Log4net、Nlog簡(jiǎn)單性能測(cè)試,比較log4net、nlog的文件寫入性能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • GridView導(dǎo)出Excel實(shí)現(xiàn)原理與代碼

    GridView導(dǎo)出Excel實(shí)現(xiàn)原理與代碼

    使用GridView來展示數(shù)據(jù)庫表,幾乎沒對(duì)GridView的格式做什么設(shè)定,從配置文件中加載SQL,跑出數(shù)據(jù)就直接綁定到GridView,接下來介紹導(dǎo)出Excel的功能感興趣的朋友可以參考下
    2013-01-01
  • 輕量級(jí)ORM框架Dapper應(yīng)用之實(shí)現(xiàn)CURD操作

    輕量級(jí)ORM框架Dapper應(yīng)用之實(shí)現(xiàn)CURD操作

    這篇文章介紹了使用Dapper實(shí)現(xiàn)CURD操作的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • asp.net 代碼隱藏的編碼模型

    asp.net 代碼隱藏的編碼模型

    asp.net 代碼隱藏的編碼模型,需要的朋友可以參考下。
    2009-11-11
  • .NET?Core使用APB?vNext框架入門教程

    .NET?Core使用APB?vNext框架入門教程

    這篇文章介紹了.NET?Core使用APB?vNext框架的入門教程,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-02-02
  • .net中的DI框架AutoFac簡(jiǎn)單介紹

    .net中的DI框架AutoFac簡(jiǎn)單介紹

    這篇文章介紹了.net中的DI框架AutoFac,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 很實(shí)用的NLog配置分享

    很實(shí)用的NLog配置分享

    這篇文章主要給大家分享介紹了關(guān)于NLog配置的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • .NET CORE HttpClient的使用方法

    .NET CORE HttpClient的使用方法

    這篇文章主要給大家介紹了關(guān)于.NET CORE HttpClient的使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用.NET CORE具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評(píng)論

富源县| 泗洪县| 丹棱县| 临朐县| 姜堰市| 山西省| 徐汇区| 繁峙县| 磴口县| 阿荣旗| 尼勒克县| 农安县| 墨江| 金塔县| 揭阳市| 荥经县| 江北区| 金华市| 绵竹市| 含山县| 军事| 盘锦市| 堆龙德庆县| 庄浪县| 长沙市| 阿拉善右旗| 望都县| 金塔县| 临海市| 梁河县| 邓州市| 印江| 五莲县| 松溪县| 肃南| 太仓市| 苏州市| 崇阳县| 濉溪县| 双牌县| 云阳县|