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??
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.
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.
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.
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.
Typo:
contoller = "Home"
should be
controller = "Home"
or:
return RedirectToAction("index", "home");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With