如何使用CSS时操作链接工作(How to work with Action Link when u

2019-07-31 06:38发布

<li class="rtsLI" id="Summary"><a href="javascript:void(0);" onclick="javascript:rtsXXX.OnClientTabSelected(this‌​, 0);" class="rtsLink"><span class="rtsTxt">Test</span></a></li> 

上面我有以下ActionLink的更换:

<li class="rtsLI" >@Html.ActionLink("test1", "Index", new { Area = "Area1", Controller = "controller1" }, new { @class = "rtsLink rtsTxt"})</li> "

在第一个CSS是工作的罚款。 但是,使用ActionLink的时候,CSS不工作。 谢谢

Answer 1:

最好的选择将是使用@Url.Action扩展方法

<li class="rtsLI" id="Summary"><a href="@Url.Action("Index", new { Area = "Tools", Controller = "UserSecurity" })" class="rtsLink"><span class="rtsTxt">User Security</span></a></li>


Answer 2:

该标准的ActionLink助手总是HTML编码的链接文本。 这意味着,如果你想里面渲染HTML,你不能使用它。 你有3种可能性:

  1. 修改你的CSS,这样你就不需要链接内,从而使跨度rtsTxt类可以直接应用于链接
  2. 编写自定义的ActionLink助手不HTML编码的文本,这将让你产生相同的标记:

     public static class ActionLinkExtensions { public static IHtmlString ActionLinkUnencoded( this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes ) { var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext); var link = new TagBuilder("a"); link.MergeAttributes(new RouteValueDictionary(htmlAttributes)); link.Attributes["href"] = urlHelper.Action(actionName, routeValues); link.InnerHtml = linkText; return new HtmlString(link.ToString()); } } 

    然后:

     <li> @Html.ActionLinkUnencoded( "<span class=\"rtsTxt\">User Security</span>", "index", new { area = "Tools", controller = "UserSecurity" }, new { @class = "rtsLink" } ) </li> 
  3. 使用Url.Action帮手:

     <li class="rtsLI"> <a href="@Url.Action("index", new { area = "Tools", controller = "UserSecurity" })" class="rtsLink"> <span class="rtsTxt">User Security</span> </a> </li> 


Answer 3:

编写代码是这样的:

<li class="rtsLI" >@Html.ActionLink("<span class='rtsTxt'>User Security</span>", "Index", new { Area = "Tools", Controller = "UserSecurity" }, new { @class = "rtsLink"})</li>`


文章来源: How to work with Action Link when using CSS