10

So I have a HttpPost only ActionResult called Edit. After doing its thing (logic etc), I want it to redirect to a different controller. Lets say the HomeController. Here it is:

[HttpPost]
public ActionResult Edit(Chair chair, string xml)
{
    if (ModelState.IsValid)
    {
        try
        {
            _repository.EditChair(chair, xml);
            return RedirectToRoute(new { contoller = "Home", action = "index"});
        }
        catch (Exception ex)
        {
            //error msg for failed edit in XML file
            ModelState.AddModelError("", "Error editing record. " + ex.Message);
        }
    }
    return View(Chair);

}

Ive tryed other things like return RedirectResult(), RedirectToAction(), RedirectToRoute("string") - but it still keeps returning the index view from the controller the Edit method is in (ChairController).

Whats the right way to do this??

casperOne
  • 73,706
  • 19
  • 184
  • 253
Kasper Skov
  • 1,954
  • 10
  • 32
  • 53
  • 1
    Here is something similar already... [How is RedirectToRoute supposed to be used?][1] Hope it helps... [1]: http://stackoverflow.com/questions/1290355/how-is-redirecttoroute-supposed-to-be-used – Jan Christian Selke Aug 31 '11 at 10:04
  • @Jan Thanks. It seems to be working for that guy in the post, but not for me :S FML – Kasper Skov Aug 31 '11 at 11:29
  • 1
    @Kasper guess I misunderstood your question. I thought you said returns to the same action – Rune FS Aug 31 '11 at 11:34
  • @David and all you other guys. Never mind. Turned out that it was VS10 that messed it up. Wierdest thing ive ever seen. Check my answer for more info. – Kasper Skov Aug 31 '11 at 11:48

2 Answers2

21

Typo:

contoller = "Home"

should be

controller = "Home"

or:

return RedirectToAction("index", "home");
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Woops. Fail typo. But yeah as stated in my question, already tryed RedirectToAction(). Doesnt work. – Kasper Skov Aug 31 '11 at 11:22
  • 1
    @Kasper Skov, unless you have messed something with your route definitions `return RedirectToAction("index", "home");` works perfectly fine. – Darin Dimitrov Aug 31 '11 at 11:51
5

Wow wierdest thing ever caused this. The code was correct (as I was sure of to begin with). I tryed to debug it once more, and noticed as I went through the code, that the debugger thingo only marked up some of the code: return RedirectToAction("Index", It actually stopped there, and didnt go through the "Home");. I also noticed that my breakpoint was actually yellow, and was telling me something about the source code was not identical with the original? The what what? It kept saying that through hundreds of saves, restarts, builds and rebuilds. Out of the blue it accepted the code, my breakpoint turned red, the code worked just fine.

Really sorry for waisting your time guys!

Kasper Skov
  • 1,954
  • 10
  • 32
  • 53