I have a get and post method for my view, and in the get method I set the value of several ViewData objects. But when I call the post method these ViewData objects become null. Do I need to reset them in my post method? Here is my code :
public ActionResult Index()
{
ViewData["afceaststandings"] = GetStandingsForGrid("2017", "AFC East");
ViewData["afccentralstandings"] = GetStandingsForGrid("2017", "AFC Central");
ViewData["afcweststandings"] = GetStandingsForGrid("2017", "AFC West");
return View("Index");
}
[HttpPost]
public ActionResult Index(QBRating qbm)
{
if (ModelState.IsValid)
{
string Result;
double dblResult;
qbm.Completion = ((qbm.Completion - 30) * 0.05);
if (qbm.Completion < 0)
{
qbm.Completion = 0;
}
if (qbm.Completion > 2.375)
{
qbm.Completion = 2.375;
}
qbm.Gain = ((qbm.Gain - 3) * 0.25);
if (qbm.Gain < 0)
{
qbm.Gain = 0;
}
if (qbm.Gain > 2.375)
{
qbm.Gain = 2.375;
}
qbm.Touchdown = (qbm.Touchdown * 0.2);
if (qbm.Touchdown > 2.375)
{
qbm.Touchdown = 2.375;
}
qbm.Interception = (2.375 - (qbm.Interception * 0.25));
if (qbm.Interception < 0)
{
qbm.Interception = 0;
}
dblResult = Math.Round((((qbm.Completion + qbm.Gain + qbm.Touchdown + qbm.Interception) / 6) * 100), 2);
Result = "QB Rating = " + Convert.ToString(dblResult);
TempData["QBRating"] = Result;
}
//invalid - redisplay form with errors
return View(qbm);
}
Yes, values will need to be re-assigned to
ViewDatain your POST controller action asViewDatais not persisted across requests.You could use
TempDatato persist data for a request (it is persisted in ViewData until next accessed); The defaultITempDataProvider(SessionStateTempDataProvider) uses SessionState so, depending on what kind of Session Store you are using, the items you are putting into TempData may need to be serializable.