標準のヘルパーのようにテキストを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) %>