Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Remove ModelState Errors

I've got a situation where I'm uploading an image the user has selected from his local file system. My Form in my view, basically has two submit buttons. One is used to Submit the form normally, and all validation executes. The 2nd is only for uploading the image, in which case I don't want to validate yet.

I managed to turn off Client Side validation by giving my 'Upload Image' submit button an a class value of "style-name cancel" , so

<input type="submit" name="UploadImageButton" value="Upload Image" class="style-name cancel" />  

Now, when I post back, my model has a property UploadImageButton, when this button is clicked, it populates this property (Name of the input matches the Property). This way, I know whether the form was submitted by my true Submit button or by the UploadImageButton.

My question is this... How can I turn off ServerSide validation? I don't want the Validation Summary info showing up when the user clicks this button. I know you can add custom model errors using this

ModelState.AddModelError("{key}", "{error msg}"); 

I'm looking for a means to Remove Model Errors. Is this possible?

EDIT:

Here is what I came up with:

foreach (var key in ModelState.Keys.ToList().Where(key => ModelState.ContainsKey(key))) {      //ModelState.Remove(key); //This was my solution before      ModelState[key].Errors.Clear(); //This is my new solution. Thanks bbak } 
like image 311
Jeff Reddy Avatar asked Sep 14 '11 22:09

Jeff Reddy


People also ask

How do I remove a ModelState error?

AddModelError() method to add a validationerror to the ModelState. If you then check the IsValid property in the ModelState variable, it will say its not valid. If you call ModelState. Clear() and then check the IsValid property again it will be valid.

What is ModelState remove?

Remove(KeyValuePair<String,ModelState>) Removes the first occurrence of the specified object from the model-state dictionary. Remove(String) Removes the element that has the specified key from the model-state dictionary.

Why is my ModelState IsValid false?

IsValid is false now. That's because an error exists; ModelState. IsValid is false if any of the properties submitted have any error messages attached to them. What all of this means is that by setting up the validation in this manner, we allow MVC to just work the way it was designed.

Why is ModelState not valid MVC?

The error typically means that your Model doesn't meet the requirements to be validated. In your example, your FirstName , LastName , Email , Subject and Message properties are decorated with the [Required] attribute. This means that those values shouldn't be null and empty otherwise your condition if(ModelState.


2 Answers

You can remove model errors by doing something like this:

if (ModelState.ContainsKey("{key}"))     ModelState["{key}"].Errors.Clear(); 
like image 117
bbak Avatar answered Sep 19 '22 03:09

bbak


This builds off previous answers, but simplifies it a little more:

foreach (var modelValue in ModelState.Values) {     modelValue.Errors.Clear(); } 
like image 36
Dan Friedman Avatar answered Sep 19 '22 03:09

Dan Friedman