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

前端 未结 8 2315
误落风尘
误落风尘 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 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();
    }
    

提交回复
热议问题