ASP.NET MVC > Models

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

  1. [Key] - to mark the property as primary key

  2. [DatabaseGenerated] - to mark the property value as generated from the database.

  3. [ScaffoldColumn] - to mark whether this property should be used as a field while scaffolding the view or not

            public int AutoId { get; set; }
  4. [Display("Name")] - to render user friendly label for this property

  5. [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

  6. [NotMapped] - to mark the property as not mapped, ie. there is no column in the database table corresponding to this property

    public DateTime? BirthDate { get; set; }
  7. [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditModel = true)] - to apply the format to display for this property value
    • [DisplayFormat(NullDisplayText = "Not specified")] - display as "Not specified" if this property field value is null

      [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
      // [DisplayFormat(NullDisplayText = "Not specified")]
      public DateTime? BirthDate { get; set; }
  8. [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).

  9. [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.

  1. [Required] - to mark a property as mandatory

  2. [StringLength(max)] - to limit the maximum number of characters allowed

  3. [DataType(DataType.type)] - to set the type of data allowed in this property

  4. [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; }
  5. [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; }
  6. [RegularExpression("pattern")] - property value must match the specified pattern

    public string Email { get; set; }
  7. [Remote(controllerName, actionMethodName) - to apply remote validation on this field

    [Remote("CheckForDuplication", "Validation")]
    public string UserName { get; set; }
  8. 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: 37608 | Post Order: 31

Write for us

Hosting Recommendations