asp.net-mvc-3 – 如何将哈希片段添加到T4MVC路由字典ActionResu
我有一个返回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中,然后再将其传递回视图: > Including hash values in ASP.NET MVC URL routes 更新: 使用下面的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",new { @class = "button blue" },fragment: tabRoute.ToString()); 我确信有一种更漂亮的方法可以使用ActionResult返回片段,但目前这是有效的.谢谢大卫. 解决方法
T4MVC需要新的重载来处理这个问题.在T4MVC.tt中,尝试更改:
public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper,ActionResult result,object htmlAttributes) { return ActionLink(htmlHelper,linkText,result,new RouteValueDictionary(htmlAttributes)); } public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper,IDictionary<string,object> htmlAttributes) { return htmlHelper.RouteLink(linkText,result.GetRouteValueDictionary(),htmlAttributes); } 至 public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper,object htmlAttributes,string protocol = null,string hostName = null,string fragment = null) { return ActionLink(htmlHelper,new RouteValueDictionary(htmlAttributes),protocol,hostName,fragment); } public static <#=HtmlStringType #> ActionLink(this HtmlHelper htmlHelper,object> htmlAttributes,string fragment = null) { return htmlHelper.RouteLink(linkText,null,fragment,htmlAttributes); } 然后你就可以写下这样的东西: @Html.ActionLink("Product Details",fragment: "#tab-similar-products") 如果有效,请告诉我,我会尝试将其添加到主模板中. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.Net多个源文件
- asp.net – 解析接受标题
- asp.net-mvc – 检测到潜在的危险Request.Form值
- asp.net-mvc – asp.net mvc子行为
- asp.net – 为什么即使我将EnableViewState设置为
- asp.net-mvc – 返回修改后的viewModel进行查看
- asp.net-mvc – 在视图中创建DropDownListFor项
- (推荐)第15章 扩展ASP.NET MVC (IModelBinder、
- 使用 ASP.NET Core MVC 创建 Web API——响应数据
- ASP.NET Core和JQuery不显眼的AJAX无法正常工作