原始 ActionLink 链接文本 [英] Raw ActionLink linkText
本文介绍了原始 ActionLink 链接文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想把一个按钮作为 @ActionLink()
的文本,但我不能,因为它 HTML 转义了我的字符串......我找到了 @Html.Raw()
机制并尝试了 @ActionLink().ToHtmlString()
但不知道如何将其组合在一起...
I want to put a button as the text of an @ActionLink()
but I can't because it HTML-escapes my string... I found the @Html.Raw()
mechanism and have tried the @ActionLink().ToHtmlString()
but can't figure out how to put it together...
我发现一篇文章,描述了为类似目的构建扩展,但它是讨厌去那么多麻烦......一定有一个简单的方法吗?
I found an article that describes building an extension for a similar purpose but it's eeky to go to that much trouble... there must be an easy way?
推荐答案
你可以写一个助手:
public static class HtmlExtensions
{
public static IHtmlString MyActionLink(
this HtmlHelper htmlHelper,
string linkText,
string action,
string controller,
object routeValues,
object htmlAttributes
)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var anchor = new TagBuilder("a");
anchor.InnerHtml = linkText;
anchor.Attributes["href"] = urlHelper.Action(action, controller, routeValues);
anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
return MvcHtmlString.Create(anchor.ToString());
}
}
然后使用这个助手:
@Html.MyActionLink(
"<span>Hello World</span>",
"foo",
"home",
new { id = "123" },
new { @class = "foo" }
)
哪个给定的默认路由会产生:
which given default routes would produce:
<a class="foo" href="/home/foo/123"><span>Hello World</span></a>
这篇关于原始 ActionLink 链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文