• @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>
  • 相关阅读:
    C#线程锁使用全功略
    viewstate 与 session 区别
    Server.MapPath() 用法
    SQL Server 存储过程
    数据库索引的概念
    从C#程序中调用非受管DLLs
    [转载]C++、C#写的WebService相互调用
    解决WCF接口无法传递object参数的问题
    UTF-8,UTF-16
    js 验证字符串是否全为中文
  • 原文地址:https://www.cnblogs.com/Kummy/p/3048946.html
Copyright © 2020-2023  润新知