Using ASP.Net MVC Data Annotation outside of MVC

Using ASP.Net MVC Data Annotation outside of MVC

i was wondering if there is a way to use ASP.Net's Data annotation without the MVC site.

My example is that i have a class that once created needs to be validated, or will throw an error. I like the data annotations method, instead of a bunch of if blocks fired by the initaliser.

Is there a way to get this to work?

I thought it would be something like:

  • Add data annotations
  • Fire a method in the initialiser that calls the MVC validator on the class

any ideas? i must admit i havent added the MVC framework to my project as i was hoping i could just use the data annotations class System.ComponentModel.DataValidation

How many files are too many in an ASP.NET MVC project?

1: mvc 2.0 Jquery form submit
Here's an example:. Pulling a View from a database rather than a file
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations;  public class Foo {     [Required(ErrorMessage = "the Bar is absolutely required :-)")]     public string Bar { receive ; set; } }  class Program {     public static void Main()     {         var foo = new Foo();         var results = new List<ValidationResult>();         var context = new ValidationContext(foo, null, null);         if (!Validator.TryValidateObject(foo, context, results))         {             foreach (var error in results)             {                 Console.WriteLine(error.ErrorMessage);             }         }     } } 
But quite honestly FluentValidation is enough more powerful.. When is it ok to meddle in the page lifecycle an ASP.NET MVC page?
Significant advantages of ASP.NET MVC

70 out of 100 based on 35 user ratings 430 reviews