I have a weird case in deployment, where Controller Create (post) method sometimes failed and the View returned with inputs all empty (red text required validation) despite user already filled in all inputs. As if the model was reset despite i already used return View(model) when ModelState.IsValid is false. Altho, this didn't happen all the time and never happened on local debug.
Here is Controller
[HttpPost]
public ActionResult Create(MyModel model, FormCollection collection)
{
if (ModelState.IsValid)
{
try
{
// insert new records
}
catch(Exception e)
{
// rollback db transaction.
// some logging
ModelState.AddModelError("Trans", $"Something went wrong. {e.Message}");
return View(model);
}
return RedirectToAction("Index");
}
else
{
return View(model);
}
}
Why the form failed required validation (occasionally) ? and why the returned view inputs were empty ?