Data Annotation

30 May 20199 minutes to read

Data Annotations help us to define the rules to the model classes or properties for data validation and displaying suitable messages to end users.

You can enable Data Annotation by binding the corresponding Class to Grid helper and thus data annotations attributes will be mapped to the corresponding Grid Column property.

Please find the following list of annotation attributes that are supported in Grid Control.

Attribute Name Functionality in Grid
BindAttribute - Exclude To exclude the corresponding field from Grid Columns property in AutoGenerate Grid
DisplayName It sets `HeaderText` property of Grid Column
ReadOnly It sets `AllowEditing` for a particular column
Key To Set `PrimaryKey` in Grid Columns
ScaffoldColumn It sets `Visible` property of Grid Columns which is used to hide or show a Column in Grid
DisplayFormat - DataFormatString To sets `Format` property of Grid Column that renders corresponding Grid Column data in respective format.
DatabaseGenerated(DatabaseGeneratedOption.Identity) To set `isIdentity` Property of Grid Column
Validation
  • RequiredAttribute
  • StringLengthAttribute
  • RangeAttribute
  • RegularExpressionAttribute
  • MinLengthAttribute
  • MaxLengthAttribute
  • CompareAttribute
  • DataTypeAttribute
  • DataType.Custom
  • DataType.Date
  • DataType.DateTime
  • DataType.EmailAddress
  • DataType.ImageUrl
  • DataType.Url
  • Custom DataType
The data annotation validations attribute would used as `validation rules` in Grid CRUD operations
ForeignKey Set `ForeignKeyField` Property of Grid Column

NOTE

Grid Properties has more priority than Data Annotation. For Instance, if DisplayName Attribute is set to a Field in Grid Model class and also we set different value to the respective Grid Columns property HeaderText, then the value of HeaderText property will be considered and shown in Grid header.

The following code example shows how data annotation works in Grid Control.

@model IEnumerable<EditableOrder>
             @(Html.EJ().Grid<EditableOrder>("FlatGrid")
                        .Datasource(Model)
                        .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing(); })
                        .ToolbarSettings(toolbar =>
                        {
                         toolbar.ShowToolbar().ToolbarItems(items =>
                            {
                                items.AddTool(ToolBarItems.Add);
                                items.AddTool(ToolBarItems.Edit);
                                items.AddTool(ToolBarItems.Delete);
                                items.AddTool(ToolBarItems.Update);
                                items.AddTool(ToolBarItems.Cancel);
                            });
                        })
                        .AllowPaging()
                  )
namespace EJGrid.Controllers
         {
        public class EditableOrder
        {
            [Display(Name = "Order ID")]
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int OrderID
            {
                get;
                set;
            }
            [Display(Name = "Emp ID")]
            [Required]
            public int? EmployeeID
            {
                get;
                set;
            }
            [Display(Name = "Freight")]
            [DisplayFormat(DataFormatString = "{0:C2}")]
            public decimal? Freight
            {
                get;
                set;
            }
            [Display(Name = "Country")]
            [Editable(false)]
            public string ShipCountry
            {
                get;
                set;
            }
            [Display(Name = "City")]
            [MinLength(3)]
            [MaxLength(10)]
            public string ShipCity
            {
                get;
                set;
            }
            [Display(Name = "Ship Name")]
            [ScaffoldColumn(false)]
            public string ShipName
            {
                get;
                set;
            }
        }
         public class HomeController : Controller
          {
           public ActionResult Index()
            {
              List<EditableOrder> data = OrderRepository.GetAllRecords() ;
              return View(data);
            }
          }
        }

The following output is displayed as a result of the above code example.

Date annotation in asp.net mvc grid control