ActionLink的原料LINKTEXT [英] Raw ActionLink linkText

查看:265
本文介绍了ActionLink的原料LINKTEXT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个按钮的 @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...

我发现描述为建设一个类似目的的扩展名的文章,但它的eeky去那么多麻烦......必须有一个简单的方法?

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());
    }
}

,然后使用这个助手:

and then use this helper:

@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的原料LINKTEXT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆