Pregunta

En una vista ASP.NET MVC me gustaría incluir un enlace del formulario:

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

Intentar incluir el elemento <span> en el campo linkText de una llamada a Html.ActionLink() termina codificándose (como es de esperar).

¿Hay alguna forma recomendada de lograr esto?

¿Fue útil?

Solución

Puede usar Url.Action para crear el enlace por usted:

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

o use Html.BuildUrlFromExpression:

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

Otros consejos

si te gusta usar Razor, esto debería funcionar:

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

Otra opción es renderizar su enlace de acción a MvcHtmlString de manera normal, utilizando HTML.ActionLink o Ajax.ActionLink (según su contexto), luego escriba una clase que tome el MvcHtmlString renderizado y piratee su texto de enlace html directamente en el MvcHtmlString ya representado y devuelve otro MvcHtmlString.

Entonces, esta es la clase que hace eso: [tenga en cuenta que el código de inserción / sustitución es MUY simple, y es posible que necesite reforzarlo para manejar más html anidado]

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

Y luego lo usarías en la vista así:

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

Este enfoque tiene la ventaja de no tener que reproducir / comprender el proceso de representación de etiquetas.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top