Html.RouteLink() HtmlHelper funziona perfettamente per i collegamenti di testo. Ma qual è il modo migliore per collegare un'immagine?Esiste un HtmlHelper ASP.NET MVC per i collegamenti immagine?
risposta
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();
}
<a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>
+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
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);
}
Tuttavia, ciò non crea un collegamento di azione immagine. Non è quello che Zack sta chiedendo? –
anche se questo non risponde direttamente alla domanda mi piace molto questo metodo per la creazione di un'estensione helper tag immagine. Grazie :) –
<%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %>
potrebbe funzionare, l'estensione dell'immagine da futures assemblaggio. Oppure fai la tua estensione.
Nessuna zuppa tag. Bello. –
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
<%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%>
Questo richiede una serie di brutte manipolazioni delle stringhe. –
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);
}
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();
}
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());
}
Non dovrebbe essere HtmlString invece di stringa? con ritorno new HtmlString (imglink.ToString()); ? – Stacker
Giusto per sottolineare che htmlHelper.Image() non è più in MVC4. –
Non posso dichiarare htmlHelper.Image() – kasim