Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear fields after post back in asp.net mvc?

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.

like image 648
chobo2 Avatar asked Nov 20 '09 03:11

chobo2


2 Answers

Try this:

if(ModelState.IsValid)
{
    ModelState.Clear();
}
return View();
like image 136
Bhushan Shah Avatar answered Sep 21 '22 04:09

Bhushan Shah


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

like image 34
Omar Avatar answered Sep 22 '22 04:09

Omar