2011-05-04 13 views
16

Si tengo en la extensión de esa manera:MVC Html Extension return string en lugar de html markup?

public static string ImageLink(this HtmlHelper htmlHelper, 
             string imgSrc, 
             string alt, 
             string actionName, 
             string controllerName, 
             object routeValues, 
             object htmlAttributes, 
             object imgHtmlAttributes) 
    { 

    return @"<img src=""../../Content/images/english.png"" /> "; 
    } 

y lo uso en una vista parcial de la siguiente manera:

@Html.ImageLink("../../Content/images/english.png","English", "ChangeCulture", "Account", new { lang = "en", returnUrl = this.Request.RawUrl }, null,null) 

tengo una salida como esta: enter image description here

Cualquier idea ¿por qué?

Respuesta

35

La razón por la que esto sucede es porque el operador @ en Razor automáticamente codifica HTML. Si se quiere evitar esta codificación es necesario utilizar un IHtmlString:

public static IHtmlString ImageLink(
    this HtmlHelper htmlHelper, 
    string imgSrc, 
    string alt, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes, 
    object imgHtmlAttributes 
) 
{ 
    return MvcHtmlString.Create(@"<img src=""../../Content/images/english.png"" />"); 
} 

que obviamente será mucho más correcto (y trabajando en todas las situaciones, no importa de dónde y cómo esta ayuda se llama) si está escrito como éste :

public static IHtmlString ImageLink(
    this HtmlHelper htmlHelper, 
    string imgSrc, 
    string alt, 
    string actionName, 
    string controllerName, 
    object routeValues, 
    object htmlAttributes, 
    object imgHtmlAttributes 
) 
{ 
    var img = new TagBuilder("img"); 
    var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); 
    img.Attributes["src"] = urlHelper.Content("~/Content/images/english.png"); 
    // Don't forget that the alt attribute is required if you want to have valid HTML 
    img.Attributes["alt"] = "English flag"; 
    return MvcHtmlString.Create(img.ToString(TagRenderMode.SelfClosing)); 
} 

y luego

@Html.ImageLink("../../Content/images/english.png","English", "ChangeCulture", "Account", new { lang = "en", returnUrl = this.Request.RawUrl }, null,null) 

funcionará correctamente.

Como alternativa, si no se puede modificar el ayudante podría utilizar @Html.Raw:

@Html.Raw(Html.ImageLink("../../Content/images/english.png","English", "ChangeCulture", "Account", new { lang = "en", returnUrl = this.Request.RawUrl }, null,null)) 
15

En su lugar, devuelva MvcHtmlString (mi muestra a continuación).

public static MvcHtmlString IconImg(this HtmlHelper htmlHelper, string icon, string title = "", string size = "16x16") { 
     string path = VirtualPathUtility.ToAbsolute("~/res/img/icons/" + size + "/" + icon + ".png"); 
     string imgHtml = "<img src='" + path + "' title='" + title + "' style='border:none' />"; 
     return new MvcHtmlString(imgHtml); 
    } 
+0

Gracias por su respuesta, usted votó demasiado :) –

+0

la MvcHtmlString() hizo maravillas para mí. Gracias –

Cuestiones relacionadas