Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6010293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:05:44+00:00 2026-05-23T02:05:44+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:05:45+00:00Added an answer on May 23, 2026 at 2:05 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HtmlHelper extension method that I would like to apply some logic
I have a utility method that uses Display Templates to generate HTML: public static
I have this extension method I created and it returns a string of all
The CollectionViewSource.GetDefaultView() method is not in Silverlight 3. In WPF I have this extension
Is it possible to create an extension method that returns the instance that is
I have a source object that looks like this: private class SourceObject { public
The problem is quite simple, I have a method that returns a list. I
I would like to create a safe sum extension method that would have the
I have a method that returns byte[] but for now would like to avoid
I'm trying to execute an extension method that returns and object of type T,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.