ASP.NET MVC > Redirect

Redirect to another action method in ASP.NET MVC

How to redirect the user to another action method from the controller action method?


To redirect the user to another action method from the controller action method, we can use RedirectToAction method.

public ActionResult Index()
        {
            return RedirectToAction("Create");
        }

Above action method will simply redirect the user to Create action method.


public ActionResult Index() { return RedirectToAction("Edit", new { id = 1 }); }

Above method will redirect the user to Edit action method with id parameter value as 1, ie it will bring the record id 1 in edit mode. So the url would look like "/{ControllerName}/Edit/1".

If we need to pass any querystring to the action method we want to redirect to, simply keep adding them into the 2nd parameter collection.

If we do not need to pass any parameter, simply remove the 2nd parameter as shown in the 1st code snippet above.

POINT OF INTEREST

Similarly, we can use various overload methods of the RedirectToAction method to redirect action method of different controller etc.


 Views: 58671 | Post Order: 56



Write for us






Hosting Recommendations