原始 ActionLink 链接文本

发布于 2024-11-08 20:18:35 字数 345 浏览 4 评论 0原文

我想将一个按钮作为 @ActionLink() 的文本,但我不能,因为它对我的字符串进行 HTML 转义...我找到了 @Html.Raw()< /code> 机制并尝试了 @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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-11-15 20:18:35

你可以编写一个 helper:

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

然后使用这个 helper:

@Html.MyActionLink(
    "<span>Hello World</span>", 
    "foo", 
    "home",
    new { id = "123" },
    new { @class = "foo" }
)

给定的默认路由将产生:

<a class="foo" href="/home/foo/123"><span>Hello World</span></a>

You could write a helper:

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>
你与清晨阳光 2024-11-15 20:18:35

如果您想创建使用 T4MVC 库的自定义操作链接,您可以编写以下代码:

    public static System.Web.IHtmlString DtxActionLink(
        this System.Web.Mvc.HtmlHelper html, string linkText,
        System.Web.Mvc.ActionResult actionResult = null,
        object htmlAttributes = null)
    {
        System.Web.Mvc.IT4MVCActionResult oT4MVCActionResult =
            actionResult as System.Web.Mvc.IT4MVCActionResult;

        if (oT4MVCActionResult == null)
        {
            return (null);
        }

        System.Web.Mvc.UrlHelper oUrlHelper =
            new System.Web.Mvc.UrlHelper(html.ViewContext.RequestContext);

        System.Web.Mvc.TagBuilder oTagBuilder =
            new System.Web.Mvc.TagBuilder("a");

        oTagBuilder.InnerHtml = linkText;

        oTagBuilder.AddCssClass("btn btn-default");

        oTagBuilder.Attributes["href"] = oUrlHelper.Action
            (oT4MVCActionResult.Action,
            oT4MVCActionResult.Controller,
            oT4MVCActionResult.RouteValueDictionary);

        oTagBuilder.MergeAttributes
            (new System.Web.Routing.RouteValueDictionary(htmlAttributes));

        return (html.Raw(oTagBuilder.ToString()));
    }

If you want to create a custom action link that uses T4MVC library, You can write the below code:

    public static System.Web.IHtmlString DtxActionLink(
        this System.Web.Mvc.HtmlHelper html, string linkText,
        System.Web.Mvc.ActionResult actionResult = null,
        object htmlAttributes = null)
    {
        System.Web.Mvc.IT4MVCActionResult oT4MVCActionResult =
            actionResult as System.Web.Mvc.IT4MVCActionResult;

        if (oT4MVCActionResult == null)
        {
            return (null);
        }

        System.Web.Mvc.UrlHelper oUrlHelper =
            new System.Web.Mvc.UrlHelper(html.ViewContext.RequestContext);

        System.Web.Mvc.TagBuilder oTagBuilder =
            new System.Web.Mvc.TagBuilder("a");

        oTagBuilder.InnerHtml = linkText;

        oTagBuilder.AddCssClass("btn btn-default");

        oTagBuilder.Attributes["href"] = oUrlHelper.Action
            (oT4MVCActionResult.Action,
            oT4MVCActionResult.Controller,
            oT4MVCActionResult.RouteValueDictionary);

        oTagBuilder.MergeAttributes
            (new System.Web.Routing.RouteValueDictionary(htmlAttributes));

        return (html.Raw(oTagBuilder.ToString()));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文