Raw ActionLink linkText

后端 未结 2 1256
南旧
南旧 2020-12-09 12:48

I want to put a button as the text of an @ActionLink() but I can\'t because it HTML-escapes my string... I found the @Html.Raw() mechanism and have

2条回答
  •  有刺的猬
    2020-12-09 13:09

    If you want to create a custom action link that uses T4MVC library, You can write the below code:

        public static System.Web.IHtmlString DtxActionLink(
            this System.Web.Mvc.HtmlHelper html, string linkText,
            System.Web.Mvc.ActionResult actionResult = null,
            object htmlAttributes = null)
        {
            System.Web.Mvc.IT4MVCActionResult oT4MVCActionResult =
                actionResult as System.Web.Mvc.IT4MVCActionResult;
    
            if (oT4MVCActionResult == null)
            {
                return (null);
            }
    
            System.Web.Mvc.UrlHelper oUrlHelper =
                new System.Web.Mvc.UrlHelper(html.ViewContext.RequestContext);
    
            System.Web.Mvc.TagBuilder oTagBuilder =
                new System.Web.Mvc.TagBuilder("a");
    
            oTagBuilder.InnerHtml = linkText;
    
            oTagBuilder.AddCssClass("btn btn-default");
    
            oTagBuilder.Attributes["href"] = oUrlHelper.Action
                (oT4MVCActionResult.Action,
                oT4MVCActionResult.Controller,
                oT4MVCActionResult.RouteValueDictionary);
    
            oTagBuilder.MergeAttributes
                (new System.Web.Routing.RouteValueDictionary(htmlAttributes));
    
            return (html.Raw(oTagBuilder.ToString()));
        }
    

提交回复
热议问题