Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Redirect to route from ActionResult

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

like image 453
Kasper Skov Avatar asked Aug 31 '11 10:08

Kasper Skov


People also ask

How do I redirect a controller to another view?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.

How do you redirect to action?

and after login, we put the data in a class Person object and we use RedirectToAction like this: return RedirectToAction("profile","person",new { personID = Person. personID}); It's working normally, but the parameter are shown in the URL.

What is redirect to route in MVC?

RedirectToRouteResult is an ActionResult that returns a Found (302), Moved Permanently (301), Temporary Redirect (307), or Permanent Redirect (308) response with a Location header. Targets a registered route. It should be used when we want to redirect to a route.

How redirect another action method in MVC?

To redirect the user to another action method from the controller action method, we can use RedirectToAction method. Above action method will simply redirect the user to Create action method.


1 Answers

Typo:

contoller = "Home"

should be

controller = "Home"

or:

return RedirectToAction("index", "home");
like image 92
Darin Dimitrov Avatar answered Sep 28 '22 04:09

Darin Dimitrov