2009-03-23 10 views

Respuesta

24

aquí la mía, es la función básica hacer algunas sobrecargas

public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes) 
    { 
     UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; 
     string imgtag = htmlHelper.Image(imgSrc, alt,imgHtmlAttributes); 
     string url = urlHelper.Action(actionName, controllerName, routeValues); 



     TagBuilder imglink = new TagBuilder("a"); 
     imglink.MergeAttribute("href", url); 
     imglink.InnerHtml =imgtag; 
     imglink.MergeAttributes(new RouteValueDictionary(htmlAttributes), true); 

     return imglink.ToString(); 

    } 
+1

¿No debería ser esto HtmlString en lugar de cadena? con return new HtmlString (imglink.ToString()); ? – Stacker

+4

Solo para señalar que htmlHelper.Image() ya no está en MVC4. –

+0

no puede declarar htmlHelper.Image() – kasim

34
<a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a> 
+0

+1 ya que es el método más rápido que solía cabo de tantos respondió aquí. Acabo de especificar la acción y el controlador en este método y funcionó bien. – nccsbim071

7

Crea tu propia extensión de ayuda.

public static string Image(this HtmlHelper helper, string src, string alt) 
{ 
    TagBuilder tb = new TagBuilder("img"); 
    tb.Attributes.Add("src", helper.Encode(src)); 
    tb.Attributes.Add("alt", helper.Encode(alt)); 
    return tb.ToString(TagRenderMode.SelfClosing); 
} 
+0

Sin embargo, esto no crea un enlace de acción de imagen. ¿No es eso lo que pide Zack? –

+0

, incluso si esto no responde directamente a la pregunta, realmente me gusta este método para crear una extensión de ayuda de etiqueta de imagen. Gracias :) –

9
<%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %> 

podría funcionar, la extensión de imagen es de los futuros de montaje. O haga su propia extensión.

+0

No tag-soup. Bonito. –

+0

Parece que no funciona. El método Html.ActionLink() parece codificar html el primer arg para que todos los corchetes angulares, etc. se escapen. – macon

3
<%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%> 
+1

Esto requiere un montón de feo manipulación de cadenas. –

5

que no tienen suficiente SO arrogancia para agregar un comentario, pero esto es un comentario sobre MiniScalope's comment above:

UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url;

lo haría Do ggest haciendo este método una extensión HtmlHelper en sí mismo (y simplificarlo), para su reutilización:

private static UrlHelper Url(this HtmlHelper helper) 
{ 
    return new UrlHelper(helper.ViewContext.RequestContext); 
} 
15

Esta es una versión actualizada que tengo de MiniScalope respuesta anterior. Estoy usando VS2010 y ASP.Net MVC 2 Vista Previa

 public static string ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes) 
    { 
     UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; 
     TagBuilder imgTag = new TagBuilder("img"); 
     imgTag.MergeAttribute("src", imgSrc); 
     imgTag.MergeAttributes((IDictionary<string, string>) imgHtmlAttributes,true); 
     string url = urlHelper.Action(actionName, controllerName, routeValues); 



     TagBuilder imglink = new TagBuilder("a"); 
     imglink.MergeAttribute("href", url); 
     imglink.InnerHtml = imgTag.ToString(); 
     imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true); 

     return imglink.ToString(); 

    } 
2

este código ha sido probado en mvc4 ...

public static MvcHtmlString ImageLink(this HtmlHelper htmlHelper, string imgSrc, string alt, string actionName, string controllerName, object routeValues, object htmlAttributes, object imgHtmlAttributes) 
    { 
     UrlHelper urlHelper = ((Controller)htmlHelper.ViewContext.Controller).Url; 
     var imgTag = new TagBuilder("img"); 
     imgTag.MergeAttribute("src", imgSrc); 
     imgTag.MergeAttributes((IDictionary<string, string>)imgHtmlAttributes, true); 
     string url = urlHelper.Action(actionName, controllerName, routeValues); 



     var imglink = new TagBuilder("a"); 
     imglink.MergeAttribute("href", url); 
     imglink.InnerHtml = imgTag.ToString(); 
     imglink.MergeAttributes((IDictionary<string, string>)htmlAttributes, true); 

     return MvcHtmlString.Create(imglink.ToString()); 

    } 
Cuestiones relacionadas