2

Problem


Hi, I'm trying to redirect to a different action in my controller when a Department can't be found. My problem is that it does not redirect immediately after I hit the code block. I'm not sure if it is the correct method to use for this, but to me it seemed like it should work. I would also prefer not using Response.Redirect(url); because that just seems a bit messy to me.

Code


if(errors.Count > 0)
{
    RedirectToAction("Index", "Department"); //Redirect to the index page, because the department could not be found
}

Additionally



Is there also a nice way to send an error to the Index() action? Would/Could/Should I do that with a viewbag for instance, or is that really only for the views?

Thanks in advance.

Bono
  • 4,757
  • 6
  • 48
  • 77
  • FWIW there's a duplicate of the first half of this question at http://stackoverflow.com/questions/20491271/mvc-5-redirecttoaction-not-redirecting?rq=1 – lc. Jul 11 '14 at 12:39
  • @lc. Cheers, couldn't find anything. Guess I was searching for the wrong criteria (namely "immediately"). – Bono Jul 11 '14 at 12:41

5 Answers5

4

RedirectToAction(...) returns a RedirectToActionResult which you in turn need to return from the controller method:

if(errors.Count > 0)
{
    //Redirect to the index page, because the department could not be found
    return RedirectToAction("Index", "Department"); 
}

As far as saving an error state, you can use the TempData container, which will survive the redirect but not persist longer. (See also Proper use of TempData in ASP.NET MVC3?). For example:

TempData["ErrorMessage"] = "The department was not found";

You can then check for data in your Index() method and act accordingly.

Community
  • 1
  • 1
lc.
  • 113,939
  • 20
  • 158
  • 187
3

You need to return the redirect result:

if(errors.Count > 0)
{
    return RedirectToAction("Index", "Department"); //Redirect to the index page, because the department could not be found
}

That way the execution of the method ends and the ActionResult is returned to the client, which in this case is a RedirectToAction result.

Essentially, in MVC one of the goals is to de-couple the code from the HTTP context. So instead of operating on the Response object directly (as with a Response.Redirect()), you simply return different response types form the action methods.

David
  • 208,112
  • 36
  • 198
  • 279
  • Thanks that does the trick! Wasn't aware of that part :) Also cheers for the explanation about `Redirect`. – Bono Jul 11 '14 at 12:35
0

Make sure you return the result

return RedirectToAction("Index", "Department"); //Redirect to the index page, because the department could not be found

IEatBagels
  • 823
  • 2
  • 8
  • 23
Eric
  • 930
  • 1
  • 6
  • 14
0

you should do like this return RedirectToAction("Action","Controller");

take a look here.

Reference

MSDN

Kaushik
  • 2,072
  • 1
  • 23
  • 31
0

send an error to the Index()

I assume you want to show error in Index(), Use TempData and render it on Index().

You can check null and show it in Index();

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79