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

简介: 时间:2017-03-10自定义HtmlHelper在Models文件夹下新建一个类“MyHtmlHelper”想法一:using System;using System.

时间: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>
但是出来的效果是这样的:


img_ad7250d401cf1c1ac917598d1e2e79d7.png
结果

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


img_60d7d618d301066d855042e2044c7fab.png
IDE中HtmlHelper的写法

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


img_2d28f34e991aa1ec0a2dfe2d1f89fb79.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>

目录
相关文章
|
8月前
|
XML Web App开发 前端开发
SAP UI5 进阶 - XML 视图里定义的 UI 控件,运行时实例化的技术细节剖析试读版
SAP UI5 进阶 - XML 视图里定义的 UI 控件,运行时实例化的技术细节剖析试读版
|
前端开发 IDE 开发工具
[原创]手把手教你写自定义HtmlHelper方法(Razor视图)
[原创]手把手教你写自定义HtmlHelper方法(Razor视图)
[原创]手把手教你写自定义HtmlHelper方法(Razor视图)
|
Web App开发 前端开发 C#
MVC进阶学习--HtmlHelper控件解析(四)
1.RenderPartialExtensions类      RenderPartialExtensions类主要扩展了一个方法 RenderPartial()      RenderPartial(string partialViewName);      RenderPartial(strin...
822 0
|
Web App开发 前端开发
MVC进阶学习--HtmlHelper控件解析(二)
1.InputExtensions类      InputExtensions类主要有5种类型的扩展方法,分别用于CheckBox控件,Hidden控件,Pass控件,RadionButton控件,TextBox控件2.
843 0
|
Web App开发 前端开发
MVC进阶学习--HtmlHelper控件解析(三)
1.LinkExtensions类      该类主要用于生成相关链接,主要扩展了ActionLink和RouteLink方法2.ActionLink       ActionLink扩展方法主要实现一个连接,共有十个重载方法      ActionLink(string linkText,stri...
783 0
|
Web App开发 前端开发
MVC进阶学习--HtmlHelper控件解析(五)
1.SelectExtensions 类      SelectExtensions 主要扩展了两种类型的方法 DropDowList和ListBox,这两个方法主要区别是后者添加了一个属性multiple="multiple",设置这个属性主要是为了能够多选2.
824 0
|
Web App开发 JavaScript 前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(五)
1.GridView使用Action代码   Code 1 public ActionResult Index() 2         { 3             CommonPage page = TempData["page"] as CommonPage; 4            ...
983 0
|
前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(一)
最近用MVC做项目的时候,感觉脱离了原有WebForm的那种编程方式,心中略有想法。在WebForm中由一个很常用的数据绑定控件GridView,我相信用过.net的同仁都会使用这个控件,在开发中的确给我们带来了不少的方便。
790 0
|
前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(二)
1.目录结构图      2.自定义集合类 Codeusing System;using System.Collections.Generic;using System.Linq;using System.
773 0
|
前端开发
MVC进阶学习--HtmlHelper之GridView控件拓展(三)
1.扩展核心代码 Code  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.
729 0