Model attributes in ASP.NET MVC
What are different types of Model attributes in ASP.NET MVC?
In earlier post, we already learnt how to create ASP.NET MVC model.
ASP.NET MVC Model attributes
There are many attributes that can be applied to ASP.NET MVC model or its properties, let's see them
- [Key] - to mark the property as primary key
- [DatabaseGenerated] - to mark the property value as generated from the database.
- [ScaffoldColumn] - to mark whether this property should be used as a field while scaffolding the view or not
[ScaffoldColumn(false)]
public int AutoId { get; set; }
- [Display("Name")] - to render user friendly label for this property
- [Column("Name")] - to specify different field in the database table to map this property to. By default, ASP.NET MVC Framework checks for the similar field name in the database as the property name
- [NotMapped] - to mark the property as not mapped, ie. there is no column in the database table corresponding to this property
[NotMapped]
public DateTime? BirthDate { get; set; }
- [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditModel = true)] - to apply the format to display for this property value
- [Table("Name")] - applied to the class lavel, used to map the model to different database table name (generally plural name of the model is considered as the database table name).
- [ForeignKey("PropertyName")] - used to specify the foreign key property name in the primary - foreign relationship
ASP.NET MVC Model validation attributes
ASP.NET MVC model has built in ability to validate itself in the view at client side and in the controller action method in the server side. To apply validation to the properties of the model, we can use validation attributes.
Following are ASP.NET MVC model validation attributes that is applied to the properties of the model.
- [Required] - to mark a property as mandatory
- [StringLength(max)] - to limit the maximum number of characters allowed
- [DataType(DataType.type)] - to set the type of data allowed in this property
- [Range(min, max)] - minimum and maximum numberic valud allowed
[Range(0, 18, ErrorMessage = "The value must be between 0 and 18")]
public int Age { get; set; }
- [Compare("OtherPropertyName")] - two property must have the same name. This property ane "OtherPropertyName" must have the same value. Generally used to get password and confirm password
[Compare("ConfirmPassword", ErrorMessage = "Sorry, both should match")]
public string Password { get; set; }
- [RegularExpression("pattern")] - property value must match the specified pattern
[RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]
public string Email { get; set; }
- [Remote(controllerName, actionMethodName) - to apply remote validation on this field
[Remote("CheckForDuplication", "Validation")]
[Required]
public string UserName { get; set; }
- Custom validation - used to apply custom logic while validating model property (value that can't be validated by using built-in validators)
Read this posts to see how to use above ASP.NET MVC model attributes.
Views: 38588 | Post Order: 31