MVC3 Redirect to route from ActionResult

Kasper Skov picture Kasper Skov · Aug 31, 2011 · Viewed 35.7k times · Source

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??

Answer

Darin Dimitrov picture Darin Dimitrov · Aug 31, 2011

Typo:

contoller = "Home"

should be

controller = "Home"

or:

return RedirectToAction("index", "home");