Reputation: 15583
I would like to know if is there any interface like IValidatableObject but for test the inputs for ModelState, something like that:
public class EditUserViewModel: IAnyInterface
{
public string Name{get;set;}
public string Age{get;set;}
public IEnumerable<ModelStateError> Validate()
{
if(//something)
yield new ModelStateError("error");
}
}
ps: I don't want to use data annotation!
Upvotes: 0
Views: 366
Reputation: 38598
Yes you can do this, but you will need to create a wrapper for your ModelState
to check if every property is valid and convert to to send to the View.
You could use a Library called Fluent Validation
. It is free, easy to integrate with asp.net mvc and a extensiable library to validate your ViewModel. Take a look:
Fluent Validation Page http://fluentvalidation.codeplex.com/
Fluent Validation Source (if you need) https://github.com/JeremySkinner/FluentValidation
ASP.NET MVC Integration: http://fluentvalidation.codeplex.com/wikipage?title=mvc&referringTitle=Documentation
Doing this, when you called on your post actions ModelState.IsValid
it will validate with the fluent validation classes you had setted for the ViewModel.
You also can integrate with a Inversion of Control like Unity, Ninject, StructureMap, etc... to check some informations on the respository before persist it.
Upvotes: 1