0

My project has a HtmlHelper which generates links (with routeValues) which always direct back to the controller/action which 'spawned' them.

Is it possible to retrieve these values from within the HtmlHelper? i.e without supplying them explicitly. This would work fine....

var url = new UrlHelper(html.ViewContext.RequestContext);
anchorBuilder.MergeAttribute("href", url.Action("Details", routeValues));

...were it not for the fact that that action won't always be "Details".

1

2 Answers 2

1

Or this:

        var controller = helper.ViewContext.RouteData.Values["Controller"].ToString();
        var action = helper.ViewContext.RouteData.Values["Action"].ToString();
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

var currentAction = html.ViewContext.RouteData.GetRequiredString("action");
var url = new UrlHelper(html.ViewContext.RequestContext);
anchorBuilder.MergeAttribute("href", url.Action(currentAction, routeValues));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.