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

简介: [原创]手把手教你写自定义HtmlHelper方法(Razor视图)

自定义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>

但是出来的效果是这样的:


image.png

完全不是我们想要的好吧(⊙o⊙)…

所以对于Razor视图,这种方法是行不通的

<b>想法二:</b>

所以我们查看一下IDE定义好的HtmlHelper方法是怎么写的


image.png

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


image.png


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>

我在这里等你:

相关文章
C#Razor小笔记和某些细节
C#Razor小笔记和某些细节
107 0
|
Web App开发 前端开发
MVC进阶学习--HtmlHelper控件解析(二)
1.InputExtensions类      InputExtensions类主要有5种类型的扩展方法,分别用于CheckBox控件,Hidden控件,Pass控件,RadionButton控件,TextBox控件2.
836 0
|
Web App开发 前端开发
MVC进阶学习--HtmlHelper控件解析(五)
1.SelectExtensions 类      SelectExtensions 主要扩展了两种类型的方法 DropDowList和ListBox,这两个方法主要区别是后者添加了一个属性multiple="multiple",设置这个属性主要是为了能够多选2.
815 0
|
Web App开发 前端开发 C#
MVC进阶学习--HtmlHelper控件解析(四)
1.RenderPartialExtensions类      RenderPartialExtensions类主要扩展了一个方法 RenderPartial()      RenderPartial(string partialViewName);      RenderPartial(strin...
820 0
|
Web App开发 前端开发
MVC进阶学习--HtmlHelper控件解析(三)
1.LinkExtensions类      该类主要用于生成相关链接,主要扩展了ActionLink和RouteLink方法2.ActionLink       ActionLink扩展方法主要实现一个连接,共有十个重载方法      ActionLink(string linkText,stri...
769 0
|
前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(四)
1.输出表头 Code 1  ///  2         /// 添加表头行 3         ///  4         ///  5         ///  6         ///  7         ///  8         ///  9         /// 10 ...
731 0
|
前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(二)
1.目录结构图      2.自定义集合类 Codeusing System;using System.Collections.Generic;using System.Linq;using System.
769 0
|
前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(一)
最近用MVC做项目的时候,感觉脱离了原有WebForm的那种编程方式,心中略有想法。在WebForm中由一个很常用的数据绑定控件GridView,我相信用过.net的同仁都会使用这个控件,在开发中的确给我们带来了不少的方便。
784 0
|
前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(三)
1.扩展核心代码 Code  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.
724 0
|
Web App开发 JavaScript 前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(五)
1.GridView使用Action代码   Code 1 public ActionResult Index() 2         { 3             CommonPage page = TempData["page"] as CommonPage; 4            ...
977 0