How can I add a hash fragment to T4MVC route dictionary ActionResult?

后端 未结 1 893
北恋
北恋 2020-12-10 19:38

I have an extension method that returns an ActionResult (simplified for demonstration purposes):

public static ActionResult GetTestActionResult(this HtmlHelp         


        
相关标签:
1条回答
  • 2020-12-10 20:25

    T4MVC needs new overloads to handle this. In T4MVC.tt, try changing:

    public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes) {
      return ActionLink(htmlHelper, linkText, result, new RouteValueDictionary(htmlAttributes));
    }
    
    public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, IDictionary<string, object> htmlAttributes) {
      return htmlHelper.RouteLink(linkText, result.GetRouteValueDictionary(), htmlAttributes);
    }
    

    to

    public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes, string protocol = null, string hostName = null, string fragment = null) {
      return ActionLink(htmlHelper, linkText, result, new RouteValueDictionary(htmlAttributes), protocol, hostName, fragment);
    }
    
    public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, IDictionary<string, object> htmlAttributes, string protocol = null, string hostName = null, string fragment = null) {
      return htmlHelper.RouteLink(linkText, null, protocol, hostName, fragment, result.GetRouteValueDictionary(), htmlAttributes);
    }
    

    you'll then be able to write something like:

    @Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" }, fragment: "#tab-similar-products")
    

    Let me know if that works, and I'll try to get it added to the main template.

    0 讨论(0)
提交回复
热议问题