Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what should a controller action look like if it just redirects?

Tags:

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?

like image 841
mrblah Avatar asked Dec 25 '09 22:12

mrblah


People also ask

How does redirect to action work?

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.

Which return type is used to redirect to an action method of another controller?

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.

How do I redirect a controller?

Use this: return RedirectToAction("LogIn", "Account", new { area = "" }); This will redirect to the LogIn action in the Account controller in the "global" area.


1 Answers

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})
      );
}
like image 137
Canavar Avatar answered Oct 11 '22 13:10

Canavar