asp.net MVC添加HtmlHelper扩展示例和用法

时间:2021-07-03 23:36:20

一、先创建一个HtmlHelper的扩展类,代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace Practice.Helper
{
public static class MvcHelper
{
public static string SayHello(this HtmlHelper htmlHelper, string str)
{
return str;
}
}
}

注意:类一定要是静态的,方法也要静态。

二、在视图里(顶部)需要引用这个helper的位置,比如:

@using Practice.Helper;

然后如下使用:

@Html.SayHello("hello")

够简单吧?