Can't figure out why client inputs still appear after submission and Model
Validation between a plain ActionResult and a separate POST...
I was previously doing my views without [HttpPost] in this manner:
public ActionResult Details(string nextButton)
{
if ((nextButton != null) && ModelState.IsValid)
return RedirectToAction("Confirm");
return View(myData);
}
NB: myData is my serialized View Model (which I am not sure if this is the
problem).
I also was previously using DataAnnotations on my properties, e.g.
[Required]. If any fields in the .cshtml view failed to validate (i.e., no
input in a required text box), upon clicking the "nextButton", the user
would be presented with the standard client-side validation error (as well
as previous items filled in would still be filled in).
If the user corrected the errors and went through to submit the
information they could re-visit that page later and it would be blank
(i.e., none of their prior inputs would be visible if they submitted).
Now, in order to localize text I've had to move away from DataAnnotations
and put validation in my controller temporarily. So I have also started
using [HttpPost] thusly:
public ActionResult Details()
{
return View(myData);
}
[HttpPost]
[ActionName("Details")]
public ActionResult DetailsPOST(string nextButton)
{
if (DetailsValidation())
{
if ((nextButton != null) && ModelState.IsValid)
return RedirectToAction("Confirm");
}
return View(myData);
}
...
private bool DetailsValidation()
{
bool validate = true;
if (String.IsNullOrEmpty(myData.FirstName))
{
AddModelError("FirstName", T("Please specify a First Name."));
validate = false;
}
if (!validate)
return false;
if (myData.FirstName.Length > 25)
{
AddModelError("FirstName",
T("First Name cannot be more than 25 letters."));
}
return ModelState.IsValid;
}
The above code works with my validations all the way to submission.
However, the problem now is if the user returns to the Details view the
data they entered populates the text boxes, whereas with the original way
(no [HttpPost] or special validation) if the user went back to the Details
view they would be presented with a fresh page (none of their data in the
text boxes).
Sorry if this isn't clear. But is there a way to have the text boxes clear
after submission, so if the user goes back its a fresh page? Or is this
just something I have to deal with given the new way I am doing things.
Thanks.
No comments:
Post a Comment