I have a action that just does some db work based on the parameter passed into it, then it redirects to another page.
What should the return type be then?
The RedirectToAction() Method This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response.
An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.
Use this: return RedirectToAction("LogIn", "Account", new { area = "" }); This will redirect to the LogIn action in the Account controller in the "global" area.
Use RedirectToRouteResult
for redirecting to same controller's action :
public RedirectToRouteResult DeleteAction(long itemId)
{
// Do stuff
return RedirectToAction("Index");
}
Or use this to redirect to another controller's action :
public RedirectToRouteResult DeleteAction(long itemId)
{
// Do stuff
return
new RedirectToRouteResult(
new RouteValueDictionary(
new {controller = "Home", action = "Index", Id = itemId})
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With