Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Model Validation in Asp.Net MVC

How do I disable Model validation for a single Action in a Controller ? Or can I do it per model by registering the model type at startup somewhere ?

I want the ModelBinder to bind to the model, but afterwards it should not perform the model validation.

The reason why i dont want validation to happen is because i am trying to move the logic from the controllers to a service layer which will be responsible for validating the models as i cant assume that models being passed to a service contains valid data.

As I understand this is the recommend approach (to not have logic in the controllers), so I find it a bit strange that i cant seem to find anything about how the model validation can be disabled (per action or per model type).

Please notice that I dont want to disable model validation for the entire webapplication (by removing the validation providers), and i dont want to disable the input validation that checks for malicious code being posted.


UPDATE

I am using .Net 4.0 and MVC 3 Preview 1

like image 788
MartinF Avatar asked Aug 26 '10 19:08

MartinF


1 Answers

Just remove the items you don´t need before checking if the model is valid

ModelState.Remove("Email");
if (ModelState.IsValid)
{
   // your logic
}
like image 54
Alfonso Muñoz Avatar answered Sep 19 '22 18:09

Alfonso Muñoz