Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - How can I add a hash fragment to T4MVC route dictionary 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);
}

I'm using this in an Html.ActionLink:

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

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

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

Something like:

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

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 hash values in ASP.NET MVC URL routes
  2. Create a T4MVC ActionLink with url fragment

UPDATE:

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...