I am wondering how do I clear fields after a postback in asp.net mvc? Like right now when validation errors occur the fields stay populated with whatever a user would entered. This is fine however, when no validation errors occur I want the all the fields to clear and a message to display.
So right now I have the success displaying by using ViewData but not sure how to get the fields to clear.
Try this:
if(ModelState.IsValid)
{
ModelState.Clear();
}
return View();
Phil Haack said:
Try calling
ModelState["value1"].Value = new
ValueProviderResult(null,
string.Empty,
CultureInfo.InvariantCulture);
before you return the view from within your controller action.
What this does is keep all the errors associated with the key "value1", but replaces the value with an empty value.
From this SO question
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With