Is there an ASP.NET MVC HtmlHelper for image links?

前端 未结 8 2311
误落风尘
误落风尘 2020-12-01 08:42

The Html.RouteLink() HtmlHelper works great for text links. But what\'s the best way to link an image?

相关标签:
8条回答
  • 2020-12-01 08:58

    Create your own helper extension.

    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 讨论(0)
  • 2020-12-01 09:04
    <a href="<%=Url.RouteUrl(...)%>"><img src="..." alt="..." /></a>
    
    0 讨论(0)
  • 2020-12-01 09:06

    I don't have enough SO swagger to add a comment, but this is a comment on MiniScalope's comment above:

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

    I would suggest making this an HtmlHelper extension method in itself (and simplify it), for reuse:

    private static UrlHelper Url(this HtmlHelper helper)
    { 
      return new UrlHelper(helper.ViewContext.RequestContext);
    }
    
    0 讨论(0)
  • 2020-12-01 09:08

    this code has been tested on 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());
    
        }
    
    0 讨论(0)
  • 2020-12-01 09:12

    Here is mine, it`s the core function make some overloads

    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();
    }
    
    0 讨论(0)
  • 2020-12-01 09:15
    <%= Html.ActionLink(Html.Image(imageUrl, imageAlt), actionName, controllerName) %>
    

    could work, the image extension is from the futures assembly. Or make your own extention.

    0 讨论(0)
提交回复
热议问题