using System.Collections.Generic;
using System.Web.Mvc;
namespace CommonTool
{
public static class MvcHtml
{
///
/// 生成包含图片的超链接
/// 注:若actionName为空,则超链接的地址为当前App的Home页;
/// 若actionName不为空,controllerName为空,则超链接地址为当前App的Home页对应的Action
///
///
///
/// 图片地址
///
///
///
///
///
///
public static MvcHtmlString ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName = "",
string controllerName = "", object routeValue = null, string imgCssStyle = "border-style: none;")
{
var urlHelper = new UrlHelper(html.ViewContext.RequestContext);
string imgUrl = urlHelper.Content(imgSrc);
TagBuilder imgTagBuilder = new TagBuilder("img");
IDictionary imgAttributes = new Dictionary();
imgAttributes.Add("src", imgUrl);
imgAttributes.Add("style", imgCssStyle); // 图片默认无边框
imgTagBuilder.MergeAttributes(imgAttributes);
string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing);
string linkUrl = urlHelper.Action(actionName, controllerName, routeValue);
TagBuilder linkTagBuilder = new TagBuilder("a") {InnerHtml = img};
linkTagBuilder.MergeAttribute("href", linkUrl);
return new MvcHtmlString(linkTagBuilder.ToString(TagRenderMode.Normal));
}
}
}