• @Url.ActionLink 和 @Url.Action


    问题:本人想在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>
  • 相关阅读:
    装完某些软件之后IE主页被https://www.hao123.com/?tn=93453552_hao_pg劫持
    Python之向函数传递元组和字典
    Python之变量作用域
    Python之循环遍历
    Python之元组、列表and 字典
    Python数据类型
    Python运算
    Python变量空间
    Python编译源文件& 代码优化
    299. Bulls and Cows
  • 原文地址:https://www.cnblogs.com/Kummy/p/3048946.html
Copyright © 2020-2023  润新知