Question

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?

Was it helpful?

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

OTHER TIPS

well, you could always make a new class, as a part of a Data access layer, with the same attributes, just put [required] where you want.

I believe your LINQ classes are partials. With MVC, you can use the "MetatDataTypeAttribute"

Like so

[MetadataType(typeof(UserMetadataSource))]
public partial class User {

}

class UserMetadataSource {
    [HiddenInput(DisplayValue = false)]
    public int UserId { get; set; }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top