Search code examples
asp.net-mvc-3linqrazorunobtrusive

Failing to get unobtrusive client validation


I figured out that property i want to be validated has to have [Required] attribute in C# (am i right?) If so -my model is linq generated class - how to add this attribute?


Solution

  • You can do it a couple of ways:

    • If it's possible, make the field non-nullable in the database. This will make the field required at the data layer.

    • Create a partial class that adds a property to your model class. Use this property instead of the database-generated property.

    For example:

    public partial class YourEntity
    {
       [Required]
       public string YourNewProperty
       {
          get { return this.TheRealProperty; }
          set { this.TheRealProperty = value; }
       }
    }
    

    Hopefully this helps