Add #id suffix to RedirectToAction() in controller ASP NET MVC

Viewed 273

Somehow I find this hard to describe, but here I go:

I have a div in my SelectClasses Razor view page with an id="id152". In order for me to show that div on the page at reload, I have to add the suffix #id152 to my page url.

<div id="id152">blabla</div>
...
..
<a href="#id152">Section 7</a>

Now my question: Is there a way to add/pass this suffix to a 'RedirectToAction()'?

public ActionResult Index()
{
    //All we want to do is redirect to the class selection page and add a suffix
    return RedirectToAction("SelectClasses", "Registration", new { id = 99 })); //add suffix here somewhere
}

So when my SelectClasses view is shown, the url looks something like this:

'[url]/SelectClasses/99#id152'
1 Answers

The RedirectToActionResult (among the rest of RedirectTo* results) is meant to be used for generation of URLs based on registered routing data.

In your case, you wish to concatenate a hash parameter value (#id152) that is not being sent to the server and only used by the browser. That's why said methods don't bother dealing with it.

I suggest you do this instead:

var redirUrl = Url.Action("SelectClasses", "Registration", new { id = 99 });
redirUrl = String.Concat(redirUrl, "#id152");

return Redirect(redirUrl);
Related