首先在Models文件夹下添加cs文件类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using System.Web.Mvc;
using Microsoft.SqlServer.Server;
namespace System.Web.Mvc//这里为了在页面中不需要引入命名空间
{
public static class MyHtmlHelperExt
{
public static string MyLabel(this HtmlHelper helper,string str)
{
return string.Format("<span>{0}</span>",str);
}
public static HtmlString MyHtmlStringLabel(this HtmlHelper helper, string str) //扩展防止被编码化
return new HtmlString(string.Format("<span>{0}</span>", str));
}
}
}
view页面使用实例:
<%:Html.MyHtmlStringLabel("你好") %>