Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to a hash from the controller using "RedirectToAction"

Hello I want to return an anchor from Mvc Controller

Controller name= DefaultController;

public ActionResult MyAction(int id) {         return RedirectToAction("Index", "region") } 

So that the url when directed to index is

http://localhost/Default/#region 

So that

<a href=#region>the content should be focus here</a> 

I am not asking if you can do it like this: How can I add an anchor tag to my URL?

like image 996
hidden Avatar asked May 21 '12 18:05

hidden


People also ask

What is difference between redirect and RedirectToAction?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.

Can we pass model in RedirectToAction?

By including a NuGet package called MvcContrib you can easily pass model or form data through a simple RedirectToAction function call inside of your controllers. The data will then be handled in the other view and be strongly typed to the model you are passing through the redirect.


1 Answers

I found this way:

public ActionResult MyAction(int id) {     return new RedirectResult(Url.Action("Index") + "#region"); } 

You can also use this verbose way:

var url = UrlHelper.GenerateUrl(     null,     "Index",     "DefaultController",     null,     null,     "region",     null,     null,     Url.RequestContext,     false ); return Redirect(url); 

http://msdn.microsoft.com/en-us/library/ee703653.aspx

like image 82
gdoron is supporting Monica Avatar answered Oct 14 '22 17:10

gdoron is supporting Monica