你可以寫不HTML編碼的文本爲標準助手就是一個自定義的ActionLink的擴展方法:
public static MvcHtmlString UnencodedActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName
)
{
var str = UrlHelper.GenerateUrl(null, actionName, null, null, null, null, new RouteValueDictionary(), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
var a = new TagBuilder("a")
{
InnerHtml = !string.IsNullOrEmpty(linkText) ? linkText : string.Empty
};
a.MergeAttribute("href", str);
return MvcHtmlString.Create(a.ToString(TagRenderMode.Normal));
}
然後:
<%= Html.UnencodedActionLink(linkText.Highlight(word), action) %>
甚至更好:
public static MvcHtmlString HighlightedActionLink(
this HtmlHelper htmlHelper,
string linkText,
string word,
string actionName
)
{
var str = UrlHelper.GenerateUrl(null, actionName, null, null, null, null, new RouteValueDictionary(), htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
var a = new TagBuilder("a")
{
InnerHtml = !string.IsNullOrEmpty(linkText) ? linkText.Highlight(word) : string.Empty
};
a.MergeAttribute("href", str);
return MvcHtmlString.Create(a.ToString(TagRenderMode.Normal));
}
然後:
<%= Html.HighlightedActionLink(linkText, word, action) %>
將源文件加入你的擴展方法請 – n8wrl 2012-08-13 16:59:28
不幸的是'ActionLink'總是對文本進行編碼,如果你不想發生這種情況,你可以爲它寫一個擴展名:http://stackoverflow.com/questions/6063467/ raw-actionlink-linktext – Matthew 2012-08-13 17:01:32