Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to redirect to the same route (from where the request came) in asp.net mvc

I'm posting a form from a partial view which is rendered via Html.RenderAction and I want after the form post to redirect to the same route from where the request came.

public ActionResult Index()
{
    return PartialView();
}

[HttpPost]
public ActionResult Index(FormModel input)
{
     ...
     return //Redirect to the same route
}

anybody knows how to do that ?

like image 284
Omu Avatar asked Sep 29 '10 13:09

Omu


2 Answers

You could retrieve the UrlReferrer from the Request object:

return Redirect(HttpContext.Request.UrlReferrer.OriginalString);
like image 199
Castrohenge Avatar answered Nov 15 '22 04:11

Castrohenge


I would add a query string to the the form post, which you can use to do the redirection in the controller action.

<form action="example.com/model/edit?returnurl=http://example.com/Form" method="post">

Controller action...

[HttpPost]
public ActionResult Index(FormModel input, string returnUrl)
{
     ...
     return Redirect(returnUrl);
}
like image 41
Jace Rhea Avatar answered Nov 15 '22 06:11

Jace Rhea