我如何添加一个哈希片段T4MVC航线字典的ActionResult? [英] How can I add a hash fragment to T4MVC route dictionary ActionResult?

查看:120
本文介绍了我如何添加一个哈希片段T4MVC航线字典的ActionResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

我在Html.ActionLink使用这个:

I'm using this in an Html.ActionLink:

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

我使用的是自定义的jQuery插件选项卡,使用这些哈希代码进行导航。我想补充,我想打开该选项卡,通过标记散列片段到URL的末尾。

I'm using a custom jQuery plugin for tabs, that uses these hash fragments for navigation. I want to add the tab which I want to open, by tagging the hash fragment onto the end of the URL.

Html.ActionLink确实有一个超载的片段,分别是:

Html.ActionLink does have an overload for the Fragment, namely:

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)?

However, that is full of nasty magic strings, which T4MVC is designed to remove. Is there anyway to add the fragment to the route dictionary in my static extension method (GetTestActionResult)?

是这样的:

return MVC.Products.Details(productId).AddRouteValue(String.Empty, "#tab-similar-products");

据我所知,有对SO两个类似的问题和答案,但他们不太为我提供了我所期待的。我需要包装片段插入的ActionResult其传递回视图之前:

I am aware that there are two similar questions and answers on SO, but they don't quite provide me with what I am looking for. I need to wrap the fragment into the ActionResult BEFORE passing it back to the view:


  1. Including在ASP.NET MVC URL路径的哈希值

  2. Create一个T4MVC ActionLink的哈希值与/磅符号)

  1. Including hash values in ASP.NET MVC URL routes
  2. Create a T4MVC ActionLink with hash/pound sign)

使用下面大卫Ebbo的修复,我做了如下修改。有点哈克,但它的作品:

UPDATE:

Using David Ebbo's fix below, I made the following changes. A bit hacky, but it works:

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

First I altered my internal function that returns an ActionResult so that it would also add the fragment as a route value (not ideal but works):

return MVC.Products.Details(productId).AddRouteValue("tab", "#tab-similar-products");

然后在视图中它会复制片段值出来的路线字典,然后删除该路径值的完整性。

Then in the view it copies that fragment value out of the route dictionary, then removes that route value for completeness.

// 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());

我敢肯定有一个prettier办法返回的ActionResult的片段,但在本运作的瞬间。感谢大卫。

I'm sure there is a prettier way to return the fragment with the ActionResult, but for the moment this works. Thanks David.

推荐答案

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

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);
}

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.

这篇关于我如何添加一个哈希片段T4MVC航线字典的ActionResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆