我有一个返回ActionResult的扩展方法(为了演示目的而简化):
public static ActionResult GetTestActionResult(this HtmlHelper htmlHelper, int productId)
{
return MVC.Products.Details(productId);
}
我在Html.ActionLink中使用它:
@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" });
我正在为标签使用自定义jQuery插件,它使用这些散列片段进行导航。我想通过将哈希片段标记到URL的末尾来添加我想要打开的选项卡。
Html.ActionLink的片段确实有overload,即:
public static MvcHtmlString ActionLink(
this HtmlHelper htmlHelper,
string linkText,
string actionName,
string controllerName,
string protocol,
string hostName,
string fragment,
Object routeValues,
Object htmlAttributes
)
然而,这充满了讨厌的魔术字符串,T4MVC旨在删除。无论如何在我的静态扩展方法(GetTestActionResult)中将片段添加到路由字典中?
类似的东西:
return MVC.Products.Details(productId).AddRouteValue(String.Empty, "#tab-similar-products");
我知道在SO上有两个类似的问题和答案,但它们并没有完全为我提供我想要的东西。我需要将片段包装到ActionResult中,然后再将其传递回视图:
使用David Ebbo的修复程序,我做了以下更改。有点hacky,但它的工作原理:
首先,我改变了我的内部函数,它返回一个ActionResult,这样它也会将片段作为路由值添加(不理想但有效):
return MVC.Products.Details(productId).AddRouteValue("tab", "#tab-similar-products");
然后在视图中,它将该片段值复制到路径字典中,然后删除该路由值以确保完整性。
// 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());
我确信有一种更漂亮的方法可以使用ActionResult返回片段,但目前这是有效的。谢谢大卫。
答案 0 :(得分: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);
}
到
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);
}
然后你就可以写出类似的内容:
@Html.ActionLink("Product Details", Html.GetTestActionResult(Model.ProductId), new { @class = "button blue" }, fragment: "#tab-similar-products")
如果有效,请告诉我,我会尝试将其添加到主模板中。