创建一个在链接文本中包含 HTML 元素的 ActionLink

发布于 2024-07-11 04:50:23 字数 348 浏览 11 评论 0原文

在 ASP.NET MVC 视图中,我想包含以下形式的链接:

<a href="blah">Link text <span>with further descriptive text</span></a>

Trying to include the element in the linkText field of a call to Html.ActionLink() 最终会被编码(正如预期的那样)。

有没有推荐的方法来实现这一目标?

In an ASP.NET MVC view I'd like to include a link of the form:

<a href="blah">Link text <span>with further descriptive text</span></a>

Trying to include the <span> element in the linkText field of a call to Html.ActionLink() ends up with it being encoded (as would be expected).

Are there any recommended ways of achieving this?

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

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

发布评论

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

评论(3

趴在窗边数星星i 2024-07-18 04:50:23

您可以使用 Url.Action 为您构建链接:

<a href="<% =Url.Action("Action", "Controller")%>">link text <span>with further blablah</span></a>

或使用 Html.BuildUrlFromExpression:

<a href="<% =Html.BuildUrlFromExpression<Controller>(c => c.Action()) %>">text <span>text</span></a>

You could use Url.Action to build the link for you:

<a href="<% =Url.Action("Action", "Controller")%>">link text <span>with further blablah</span></a>

or use Html.BuildUrlFromExpression:

<a href="<% =Html.BuildUrlFromExpression<Controller>(c => c.Action()) %>">text <span>text</span></a>
万人眼中万个我 2024-07-18 04:50:23

如果你喜欢使用 Razor,这应该可以:

<a href="@Url.Action("Action", "Controller")">link text <span>with further blablah</span></a>

if you like using Razor, this should work:

<a href="@Url.Action("Action", "Controller")">link text <span>with further blablah</span></a>
等风也等你 2024-07-18 04:50:23

另一种选择是使用 HTML.ActionLink 或 Ajax.ActionLink(具体取决于您的上下文)将您的操作链接呈现为 MvcHtmlString,然后编写一个类,该类采用呈现的 MvcHtmlString 并将您的 html 链接文本直接写入已经呈现 MvcHtmlString,并返回另一个 MvcHtmlString。

所以这是执行此操作的类:[请注意,插入/替换代码非常简单,您可能需要加强它以处理更多嵌套的 html]

namespace Bonk.Framework
{
    public class CustomHTML
    {
        static public MvcHtmlString AddLinkText(MvcHtmlString htmlString, string linkText)
        {
            string raw = htmlString.ToString();

            string left = raw.Substring(0, raw.IndexOf(">") + 1);
            string right = raw.Substring(raw.LastIndexOf("<"));

            string composed = left + linkText + right;

            return new MvcHtmlString(composed);
        }
    }
}

然后您将在视图中使用它,如下所示:

@Bonk.Framework.CustomHTML.AddLinkText(Ajax.ActionLink("text to be replaced", "DeleteNotificationCorporateRecipient"), @"Link text <span>with further descriptive text</span>")

这种方法具有不必重现/理解标签渲染过程的优点。

Another option is to render your action link to an MvcHtmlString as per normal, using either HTML.ActionLink, or Ajax.ActionLink (depending on your context), then write a class that takes the rendered MvcHtmlString and hacks your html link text directly into the already rendered MvcHtmlString, and returns another MvcHtmlString.

So this is the class that does that: [please note that the insertion/substitution code is VERY simple, and you may need to beef it up to handle more nested html]

namespace Bonk.Framework
{
    public class CustomHTML
    {
        static public MvcHtmlString AddLinkText(MvcHtmlString htmlString, string linkText)
        {
            string raw = htmlString.ToString();

            string left = raw.Substring(0, raw.IndexOf(">") + 1);
            string right = raw.Substring(raw.LastIndexOf("<"));

            string composed = left + linkText + right;

            return new MvcHtmlString(composed);
        }
    }
}

And then you would use it in the view like this:

@Bonk.Framework.CustomHTML.AddLinkText(Ajax.ActionLink("text to be replaced", "DeleteNotificationCorporateRecipient"), @"Link text <span>with further descriptive text</span>")

This approach has the advantage of not having to reproduce/understand the tag-rendering process.

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