BrokeMyLegBiking
BrokeMyLegBiking

Reputation: 5988

Model validation for any arbitrary model

Validation attributes in System.ComponentModel.DataAnnotations is very helpful in ASP.NET MVC 3.

There is a built in method called ModelState.IsValid which will tell me whether the current model bound to the view is valid or not, but want if I want to validate any arbitrary model that uses Validation Attributes. I am looking for a helper method that looks something like this:

Pseudo-code:

List<RuleViolation> brokenRules = ValidationHelper.GetValidationErrors(customer);

I know there has to be such a helper method in the System.ComponentModel namespace, or the ASP.NET MVC 3 namespaces, but I can’t find it.

Upvotes: 0

Views: 606

Answers (2)

Quang Vo
Quang Vo

Reputation: 492

In general you should use Validator class to validate an object, or a property of it.

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532435

Look at the ModelState.Errors property. This should contain any model errors if the model state is invalid.

Upvotes: 1

Related Questions