如何将哈希片段添加到T4MVC路由字典ActionResult?

Reb*_*cca 4 t4mvc asp.net-mvc-3

我有一个返回ActionResult的扩展方法(为了演示目的而简化):

public static ActionResult GetTestActionResult(this HtmlHelper htmlHelper, int productId)
{
    return MVC.Products.Details(productId);
}
Run Code Online (Sandbox Code Playgroud)

我在Html.ActionLink中使用它:

@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" });
Run Code Online (Sandbox Code Playgroud)

我正在为标签使用自定义jQuery插件,它使用这些散列片段进行导航.我想通过将哈希片段标记到URL的末尾来添加我想要打开的选项卡.

Html.ActionLink确实有一个Fragment 的重载,即:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    string protocol,
    string hostName,
    string fragment,
    Object routeValues,
    Object htmlAttributes
)
Run Code Online (Sandbox Code Playgroud)

然而,这充满了讨厌的魔术字符串,T4MVC旨在删除.无论如何在我的静态扩展方法(GetTestActionResult)中将片段添加到路由字典中?

就像是:

return MVC.Products.Details(productId).AddRouteValue(String.Empty, "#tab-similar-products");
Run Code Online (Sandbox Code Playgroud)

我知道SO上有两个类似的问题和答案,但它们并不能完全满足我的要求.我需要将片段包装到ActionResult中,然后再将其传递回视图:

  1. 在ASP.NET MVC URL路由中包含哈希值
  2. 使用url片段创建T4MVC ActionLink

更新:

使用下面的David Ebbo修复程序,我进行了以下更改.有点hacky,但它的工作原理:

首先,我改变了我的内部函数,它返回一个ActionResult,这样它也会将片段作为路由值添加(不理想但有效):

return MVC.Products.Details(productId).AddRouteValue("tab", "#tab-similar-products");
Run Code Online (Sandbox Code Playgroud)

然后在视图中,它将该片段值复制到路径字典中,然后删除该路由值以获得完整性.

// get my ActionResult with the tab fragment tagged on as a route value
var actionResult = Html.GetTestActionResult(item.Key, Model.ClaimId);

// get the tab fragment value
var tabRoute = actionResult.GetRouteValueDictionary().FirstOrDefault(r => r.Key == "tab").Value ?? "none";

// remove the route value, otherwise it will get tagged to the querystring
actionResult.GetRouteValueDictionary().Remove("tab");

// display
@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" }, fragment: tabRoute.ToString());
Run Code Online (Sandbox Code Playgroud)

我确信有一种更漂亮的方法可以使用ActionResult返回片段,但目前这是有效的.谢谢大卫.

Dav*_*bbo 6

T4MVC需要新的重载来处理这个问题.在T4MVC.tt中,尝试更改:

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);
}
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

然后你就可以写下这样的东西:

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

如果有效,请告诉我,我会尝试将其添加到主模板中.

  • 我刚刚将这个更改推送到NuGet的T4MVC 2.6.56 (5认同)