I’m experiencing a NullReferenceException when I attempt a RedirectToAction after completing a POST Action. Here is a summary of what I’m attempting:
After an Application is POST’ed, if the Application is valid I want the client redirected to another View (and Route) for follow-on processes.
[HttpPost]
public ActionResult SubmitApplication(Application application)
{
// Do stuff
if (!ModelState.IsValid)
{
return View(application);
}
// Create and set variables for the next route
return RedirectToAction("ApplicationReview", new
{
x = "foo",
y = "bob"
});
}
[HttpGet]
public ActionResult ApplicationReview(string x, string y)
{
// Do stuff using x and y as keys
return View();
}
In the SubmitApplication View I have code similar to the following, although it is more elaborate. After the RedirectToAction is called the Model object in the SubmitApplication View is null and throws an exception. I can sort-of understand why the SubmitApplication View is returned as part of the redirect process, I just can’t figure out why the Model is null or how to set it. In the end, the target Action is never reached presumably because the redirect never really occured.
@if (Model.HasSomething)
{
...
}
What am I doing wrong? Is there a better way to do this?
The second parameter in RedirectToAction is not for the model, but for the routeValues.
To maintain state temporarily for a redirect result, you need to store your data in TempData.