@Url.ActionLink 和 @Url.Action

博客提出在MVC的ActionLink中把“继续学习”换成图片显示,却一直按字符串处理的问题。给出两种解决方法,一是重写ActionLink进行处理,二是改成Url.Action。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:本人想在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>

 

 

转载于:https://www.cnblogs.com/Kummy/archive/2013/05/02/3048946.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值