How to work with Action Link when using CSS

前端 未结 3 1217
情深已故
情深已故 2021-01-26 10:10
  • 3条回答
    •  忘了有多久
      2021-01-26 10:44

      The standard ActionLink helper always HTML encodes the link text. This means that you cannot use it if you want to render HTML inside. You have 3 possibilities:

      1. Modify your CSS so that you don't need a span inside the link and so that the rtsTxt class could directly be applied to the link
      2. Write a custom ActionLink helper that doesn't HTML encode the text and which would allow you to generate the same markup:

        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());
            }
        }
        

        and then:

      3. @Html.ActionLinkUnencoded( "User Security", "index", new { area = "Tools", controller = "UserSecurity" }, new { @class = "rtsLink" } )
      4. Use the Url.Action helper:

      5. User Security

    提交回复
    热议问题