a developer's notes – a semi-technical web development BLOG

September 4, 2013

In ASP.NET MVC, What is ModelState.Clear()?

Filed under: ASP.NET MVC — Duy Nguyen @ 9:16 pm
Tags: , , , , , ,

From http://patrickdesjardins.com/blog/modelstate-clear-is-required-to-display-back-your-model-object#comment-3814

“If you are getting your Model from a form and you want to manipulate the data that came from the client form and write it back to a view, you need to call ModelState.Clear() to clean the ModelState values.

The reason is that normally, you want to postback to the client the form with all the errors. So, when you put back the parameter that contain your model to the view to be returned, this one use the value of the ModelState.”

ModelState.Clear() is used to clear errors but it is also used to force the MVC engine to rebuild the model to be passed to your View.

So call ModelState.Clear() right before you pass the model to your View.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: