0

If I do a post to "DoAny" action and in the action call another action called "OtherAction", the url in the browser stays on the url of "DoAny" action. But it should change to url "other/action" in the browser too. How to accomplish this?

 [Route("signup/finished")]
 public ActionResult DoAny([Deserialize] RegistrationModel rm){
  return OtherAction(rm);                                       
}

 [Route("other/action")]
 public ActionResult OtherAction([Deserialize] RegistrationModel rm){
  return View(rm);                                      
}
  1. request to domain/signup/finished
  2. returns view of OtherAction
  3. But the url still points to domain/signup/finished and not to domain/other/action
Legends
  • 21,202
  • 16
  • 97
  • 123

1 Answers1

1

You can return a RedirectResult

[Route("signup/finished")]
public ActionResult DoAny([Deserialize] RegistrationModel rm)
{
   return RedirectToActoin("OtherAction");
}

this sends a 302 response to the browser with the new url as the location header value and browser will make a totally new Http request to that action method. So you should not try to pass a complex object.

If the view model data you want to pass is a lean-simple DTO, you can pass that as the routedata parameter. the framework will conver the DTO to querystring and send it.

return RedirectToActoin("OtherAction",rm);  

If the object is complex, you should consider some sort of persistence and read it back in the next action method.

You should also consider PRG pattern. PRG stands for POST - REDIRECT - GET. With this approach, you will issue a redirect response with a unique id in the querystring, using which the second GET action method can query the resource again and return something to the view.

[Route("signup/finished")]
public ActionResult DoAny([Deserialize] RegistrationModel rm)
{
    // to do : Save to db
    var newUserId= 101; //replace with the newly inserted id
    return RedirectToAction("OtherAction", "Account", new { userId=newUserId} ); 
}      
public ActionResult OtherAction(int userId)
{
  // to do : Get data from userId and build rm object
  return View(rm);
}

and in the OtherAction, you will query the data using this unique id and get the data needed.

Also take a look at How do I include a model with a RedirectToAction?

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • yes, but I need the model to be passed... I tried `return RedirectToActoin("OtherAction", model); ` But model is always null! I have no session available... – Legends Nov 02 '16 at 14:15
  • Perhaps, one thing yet, what is so bad about using `TempData` ? I think it shouled be disposed after the redirect has finished... – Legends Nov 02 '16 at 14:53
  • Yes. It will be. So what happens when user reloads the page (press f5)? That is the reason PRG is a good practice – Shyju Nov 02 '16 at 15:01
  • He gets a blank form in my case, because it's a fresh request, this is the natural behaviour ... – Legends Nov 02 '16 at 15:15