在链接文本中使用HTML元素创建一个ActionLink

时间:2022-07-12 01:27:07

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

在一个ASP。我想包含一个表单的链接:

<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).

尝试将元素包含在一个调用Html.ActionLink()的链接文本字段中,结果是它被编码(正如所期望的那样)。

Are there any recommended ways of achieving this?

有没有什么推荐的方法来达到这个目的?

3 个解决方案

#1


48  

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

您可以使用Url。行动为你建立联系:

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

or use Html.BuildUrlFromExpression:

或者使用Html.BuildUrlFromExpression:

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

#2


39  

if you like using Razor, this should work:

如果你喜欢用剃刀,这个应该可以:

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

#3


0  

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.

另一种选择是按照常规使用HTML将操作链接呈现到MvcHtmlString。ActionLink或Ajax。ActionLink(取决于您的上下文),然后编写一个类,该类接受已呈现的MvcHtmlString并将html链接文本直接修改为已呈现的MvcHtmlString,然后返回另一个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]

这是实现这一点的类:[请注意,插入/替换代码非常简单,您可能需要加强它以处理更多嵌套的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.

这种方法的优点是不需要复制/理解标记呈现过程。

#1


48  

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

您可以使用Url。行动为你建立联系:

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

or use Html.BuildUrlFromExpression:

或者使用Html.BuildUrlFromExpression:

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

#2


39  

if you like using Razor, this should work:

如果你喜欢用剃刀,这个应该可以:

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

#3


0  

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.

另一种选择是按照常规使用HTML将操作链接呈现到MvcHtmlString。ActionLink或Ajax。ActionLink(取决于您的上下文),然后编写一个类,该类接受已呈现的MvcHtmlString并将html链接文本直接修改为已呈现的MvcHtmlString,然后返回另一个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]

这是实现这一点的类:[请注意,插入/替换代码非常简单,您可能需要加强它以处理更多嵌套的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.

这种方法的优点是不需要复制/理解标记呈现过程。