Nyprez Nyprez - 23 days ago 8x
C# Question

MVC - Is validating in ViewModel enough?

As I'm using database first I validate my inputs via ViewModel. But is that good enough for validation or is there some unwritten rule to add it e.g. in controller?

Also, what are the most common for attribute-validation, lets say password?

public string Password { get; set; }

Is this a good validation?


The attribute validation is perfectly good for what it can do. In addition to the validation attributes you mention, you have the EmailAddress attribute which validates the given property as an e-mail address.

[EmailAddress(ErrorMessage = "Invalid Email Address")]

Then you have the RegularExpression validation attribute for doing more complicated custom validations:

         ErrorMessage = "Characters are not allowed.")]

For conditional validation and such things, you can add that logic to your controller and add errors to the ModelState as needed.


if (MyProperty == "something" && MyOtherProperty != "something")
    ModelState.AddModelError("", "MyOtherProperty needs to be something")

If you don't want such boilerplate validation code in your controller logic, you can write your own custom validation attributes. By doing so, you can pretty much validate whatever you can think of.

There's plenty of resources about that topic on the Internet.

How to create a custom validation attribute?

How to create Custom Data Annotation Validators

To name a few.