美文网首页dotnet core.NET代码改变世界
手把手教你写自定义HtmlHelper方法(Razor视图)

手把手教你写自定义HtmlHelper方法(Razor视图)

作者: 垃圾简书_吃枣药丸 | 来源:发表于2017-03-10 21:29 被阅读663次

    时间:2017-03-10
    自定义HtmlHelper
    在Models文件夹下新建一个类<code>“MyHtmlHelper”</code>
    <b>想法一:</b>

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace System.Web.Mvc.Html
    {
         public static string MyTextBox(this HtmlHelper htmlHelper, string value)
            {
                return string.Format("<input type=\"text\" value={0}/>",value);
            }
    }
    

    在前端进行调用
    <code>@Html.MyTextBox("老司机")</code>
    但是出来的效果是这样的:


    结果

    完全不是我们想要的好吧(⊙o⊙)…
    所以对于Razor视图,这种方法是行不通的
    <b>想法二:</b>
    所以我们查看一下IDE定义好的HtmlHelper方法是怎么写的


    IDE中HtmlHelper的写法

    所以我们把返回值做成MvcHtmlString类型,再来试试


    代码补全提示
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace System.Web.Mvc.Html
    {
         public static MvcHtmlString MyTextBox(this HtmlHelper htmlHelper, string value)
            {
                //既然返回值类型都不是string类型了,所以我们return一个MvcHtmlString类型
                //在写MvcHtmlString的时候我们可以看到代码自动补全已经给出了一个提示,所以我们就用它试试效果
                //根据提示:使用指定文本值创建 HTML 编码的字符串,所以我们应该传入一个字符串,所以接下来的问题也就是我们需要把<input />做成一个字符串的形式
               return MvcHtmlString.Create();
            }
    }
    

    就像这样:

    //用到了转义字符
    string t=string.Format("<input type=\"text\" value=\"{0}\"/>", text);
    

    所以我们自定义的HtmlHelper辅助方法应该长成这样:

            public static MvcHtmlString MyTextBox(this HtmlHelper htmlHelper, string text)
            {
                string t=string.Format("<input type=\"text\" value=\"{0}\"/>", text);
                return MvcHtmlString.Create(t);
            }
    

    我们可以再进行几次重载

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace System.Web.Mvc.Html
    {
        public static class MyHtmlHelper
        {
            /// <summary>
            /// 自定义的TextBox
            /// </summary>
            /// <param name="htmlHelper">扩展方法,不用传值</param>
            /// <param name="text">对应input的text</param>
            /// <returns></returns>
            public static MvcHtmlString MyTextBox(this HtmlHelper htmlHelper, string text)
            {
                string t=string.Format("<input type=\"text\" value=\"{0}\"/>", text);
                return MvcHtmlString.Create(t);
            }
            /// <summary>
            /// 自定义的TextBox
            /// </summary>
            /// <param name="htmlHelper">扩展方法,不用传值</param>
            /// <param name="value">对应input的value</param>
            /// <param name="text">对应input 的text</param>
            /// <returns></returns>
            public static MvcHtmlString MyTextBox(this HtmlHelper htmlHelper, string value,string text)
            {
                string t = string.Format("<input type=\"text\" value=\"{0}\" text=\"{1}\"/>",value,text);
                return MvcHtmlString.Create(t);
            }
    
        }
    }
    
    

    <b>谢谢O(∩_∩)O~</b>

    相关文章

      网友评论

      • RenFuShuai:后面的和第一种,我怎么没看出来区别。不都是生成HTML标签吗,为什么第一种不是我们想要的😊求大神解答
        垃圾简书_吃枣药丸: @RenFuShuai 第一种没有生成Html标签的呀,应该是被编码了,直接是在页面上生成了一段文本,见图一。

      本文标题:手把手教你写自定义HtmlHelper方法(Razor视图)

      本文链接:https://www.haomeiwen.com/subject/ggvogttx.html