1. Model Task
Model is responsible for obtaining data through databases, AD (Active Directory), Web service, and other means, as well as saving user-entered data to databases, AD, Web service, and so on.
Model only focuses on effectively providing data access mechanisms, data format validation, business logic validation, and more.
2. Define Model Metadata
Metadata is used to define related properties of the data model, such as display name, data length, and data format validation. Use the dataannotations mechanism in System.ComponentModel.DataAnnotations to assist in defining the ASP. NET MVC data model.
The validation properties of the System.ComponentModel.DataAnnotations namespace include: Stringlength, Required, RegularExpression, and range.
Example:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.ComponentModel.DataAnnotations;namespacelibing.portal.web.models{ Public classProduct { Public intProductID {Get;Set; } [Required] [Stringlength ( -, errormessage="Product name Maximum length 100 characters")] Public stringProductName {Get;Set; } [RegularExpression (@"^\d+$", errormessage ="the inventory quantity can only be numeric")] [Range (0, -, errormessage ="Stock Qty 0 to 100")] Public intUnitsInStock {Get;Set; } }}
ASP. NET MVC series: Model