问题:本人想在actionlink中,把"继续学习",换成一张图片来显示,结果发现都一直是字符串来处理,mvc没有帮我们做这个事。
@Html.ActionLink("继续学习", "Learning", "Item", "http", strHostNameKaoji, "", new { itemID = item.ItemID, resourceID = item.LastStudyUserResID }, new { })
解决方法:
第一种:要么自己重写下ActionLink做下处理
public static MvcHtmlString ActionLinkWithImage(this HtmlHelper html, string imgSrc, string img_className, string img_width, string img_height,
string actionName, string controllerName, System.Web.Routing.RouteValueDictionary routeValues, string protocolName, string hostname) { var urlHelper = new UrlHelper(html.ViewContext.RequestContext); string imgUrl = urlHelper.Content(imgSrc); TagBuilder imgTagBuilder = new TagBuilder("img"); imgTagBuilder.MergeAttribute("src", imgUrl); imgTagBuilder.MergeAttribute("class", img_className); imgTagBuilder.MergeAttribute("width", img_width); imgTagBuilder.MergeAttribute("height", img_height); string img = imgTagBuilder.ToString(TagRenderMode.SelfClosing); string url = urlHelper.Action(actionName, controllerName, routeValues, protocolName, hostname); TagBuilder tagBuilder = new TagBuilder("a") { InnerHtml = img }; tagBuilder.MergeAttribute("href", url); return new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal)); }
@Html.ActionLinkWithImage(item.Res_Item.Img.ToCDN(), "", "63", "63", "Info", "Item", new RouteValueDictionary(querystringDic), "http", strHostNameKaoji)
第二种:还是老老实实改成Url.Action
@{ var parsed = HttpUtility.ParseQueryString("itemID = "+item.ItemID+"&&resourceID = "+item.LastStudyUserResID); Dictionary<string, object> querystringDic = parsed.AllKeys.ToDictionary(k => k, k => (object)parsed[k]);} <a href="@Url.Action("Learning", "Item", new RouteValueDictionary(querystringDic), "http", strHostNameKaoji)"> <img width="63" height="63" src="@(item.Res_Item.Img.ToCDN())" /> </a>