2009-03-23 21 views

risposta

24

qui la mia, la sua funzione principale fare alcuni sovraccarichi

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

Non dovrebbe essere HtmlString invece di stringa? con ritorno new HtmlString (imglink.ToString()); ? – Stacker

+4

Giusto per sottolineare che htmlHelper.Image() non è più in MVC4. –

+0

Non posso dichiarare htmlHelper.Image() – kasim

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

+1 in quanto questo è il metodo più veloce che ho usato da tanti risposto Qui. Ho appena specificato l'azione e il controller in questo metodo e ha funzionato correttamente. – nccsbim071

7

Crea la tua estensione di supporto.

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

Tuttavia, ciò non crea un collegamento di azione immagine. Non è quello che Zack sta chiedendo? –

+0

anche se questo non risponde direttamente alla domanda mi piace molto questo metodo per la creazione di un'estensione helper tag immagine. Grazie :) –

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

potrebbe funzionare, l'estensione dell'immagine da futures assemblaggio. Oppure fai la tua estensione.

+0

Nessuna zuppa tag. Bello. –

+0

Non sembra funzionare. Il metodo Html.ActionLink() sembra codificare in html il primo argomento in modo tale che tutte le parentesi angolari ecc. Vengano sfuggite. – macon

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

Questo richiede una serie di brutte manipolazioni delle stringhe. –

5

Io non hanno abbastanza SO spavalderia per aggiungere un commento, ma questo è un commento sulla MiniScalope's comment above:

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

lo farei su ggest rendendo questo metodo un'estensione HtmlHelper in sé (e semplificare), per il riutilizzo:

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

Questa è una versione aggiornata che ho dal MiniScalope risposta sopra. Sto usando VS2010 e ASP.Net MVC 2 Anteprima

 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

questo codice è stato testato su 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()); 

    } 
Problemi correlati