Question

Le Html.RouteLink () HtmlHelper fonctionne très bien pour les liens texte. Mais quelle est la meilleure façon de lier une image?

Était-ce utile?

La solution

Voici le mien, il s `la fonction de base faire quelques surcharges

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

Autres conseils

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

Ceci est une version mise à jour que j'ai MiniScalope réponse ci-dessus. J'utilise VS2010 et ASP.Net MVC 2 Aperçu

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

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

pourrait fonctionner, l'extension d'image est de l'ensemble à terme. Ou faire votre propre extention.

Créez votre propre extension d'aide.

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

Je n'ai pas assez SO fanfaronnades pour ajouter un commentaire, mais ceci est un commentaire sur Commentaire MiniScalope ci-dessus :

  

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

Je suggère ce qui en fait une méthode d'extension HtmlHelper en elle-même (et simplifier), pour la réutilisation:

private static UrlHelper Url(this HtmlHelper helper)
{ 
  return new UrlHelper(helper.ViewContext.RequestContext);
}
<%= Html.RouteLink("PLACEHOLDER", ...).Replace("PLACEHOLDER", "<img src=""..."" alt=""..."" />")%>

ce code a été testé sur 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());

    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top