一、先创建一个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")
够简单吧?