Reputation: 2405
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?
Upvotes: 0
Views: 83
Reputation: 9494
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
Upvotes: 1
Reputation: 2780
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; }
}
Upvotes: 0
Reputation: 819
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.
Upvotes: 0