TempData not persisting from Controller to View Umbraco 6.1.6 MVC
Hello All.
I am trying to return a success / fail message to my view using TempData, but TempData is always null once returned to the view. On a side note, in desperation, I also tried ViewData and even tried to use Session variables, but they failed as well. Am i missing something?
Generally speaking when a form is successful you would redirect (this prevents people from resubmitting the form accidentally with a page refresh), when it contains errors you do not redirect. I'm sure you've read through the docs here: http://our.umbraco.org/documentation/Reference/Templating/Mvc/forms.
When you redirect then ViewData/ViewBag is not persisted whereas TempData is persisted - this is actually the entire point of TempData, to persist after a single redirect.
In your controller above:
This is correct:
if (!captchaValid)
{
ModelState.AddModelError("captcha-error", captchaErrorMessage);
return CurrentUmbracoPage();
}
When ModelState.IsValid, you'd want to redirect and use TempData:
When ModelState is not valid, you'd want to not redirect and use ViewData/ViewBag. Though since ModelState is already invalid you'll already have model state errors so adding yet another error message may not be necessary:
else
{
ViewData["submit-status"] = "There was an error while sending the request";
return CurrentUmbracoPage();
}
If you do not want to redirect on success because you want to keep ModelState then you can do this instead:
if (ModelState.IsValid)
{
....
//TempData should also persist without a redirect too but generally TempData is only used
// for redirect purposes.
ViewData["submit-status"] = "Request send successfully";
return CurrentUmbracoPage();
}
TempData not persisting from Controller to View Umbraco 6.1.6 MVC
Hello All.
I am trying to return a success / fail message to my view using TempData, but TempData is always null once returned to the view. On a side note, in desperation, I also tried ViewData and even tried to use Session variables, but they failed as well. Am i missing something?
Here is my Controller Code:
Here is my view:
Hi Phillip,
Generally speaking when a form is successful you would redirect (this prevents people from resubmitting the form accidentally with a page refresh), when it contains errors you do not redirect. I'm sure you've read through the docs here: http://our.umbraco.org/documentation/Reference/Templating/Mvc/forms.
When you redirect then ViewData/ViewBag is not persisted whereas TempData is persisted - this is actually the entire point of TempData, to persist after a single redirect.
In your controller above:
This is correct:
When ModelState.IsValid, you'd want to redirect and use TempData:
When ModelState is not valid, you'd want to not redirect and use ViewData/ViewBag. Though since ModelState is already invalid you'll already have model state errors so adding yet another error message may not be necessary:
If you do not want to redirect on success because you want to keep ModelState then you can do this instead:
is working on a reply...