@Url.ActionLinkおよび@Url.Action

7101 ワード

質問:私はactionlinkで、「勉強を続けます」を1枚の画像に変えて表示したいと思っていましたが、結局文字列で処理されていて、mvcは私たちにこのことを手伝ってくれなかったことに気づきました. 
@Html.ActionLink("    ", "Learning", "Item", "http", strHostNameKaoji, "", new { itemID = item.ItemID, resourceID = item.LastStudyUserResID },
    new { })

解決方法:
  1つ目は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)

 
  2つ目:やはりおとなしく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>