ModelMetaData is an important concept in MVC, including but not limited to the type of model, what attributes the model contains, what types of attributes are there, and what characteristics are on the attributes.
ASP.NET MVC3.0 provides the default model metadata DataAnnotationsModelMetadata inherits from ModelMetadata (in addition, the system provides the default model metadata provider DataAnnotationsModelMetadataProvider)
public class DataAnnotationsModelMetadata : ModelMetadata
Its constructor is as follows
public DataAnnotationsModelMetadata(DataAnnotationsModelMetadataProvider provider, Type containerType, Func modelAccessor, Type modelType, string propertyName, DisplayColumnAttribute displayColumnAttribute);
The DataAnnotationsModelMetadata class has several commonly used properties
DisplayName display name, DisplayFormatString format string
TemplateHint gets a value to choose which template to use
For DisplayName, the LabelFor<TModel, TValue> method uses this property to generate the label text. It meansOnce the DispalyName property is defined on our model, then use the html.editForModel() method in viewIt will automatically display the label tag " <label for="username" > name </ for this attribute</label> of the model.
[display( name="name",order=2)] //order attribute changes the order of the html page public string username { get; set; }
HTML source code:
The Display property used on the username attribute is the System.ComponentModel.DataAnnotations.DisplayAttribute class, which provides a generic feature that allows you to specify localizable strings for the type and member of the entity division class
There are also features from System.Web.MVC and System.ComponentModel.DataAnnotations, whose names are all feature classes similar to XXXXAttribute, all of which have specific scopes of use, some restrict model properties, some restrict Contrller, some restrict Actions, etc.
The following is a list of built-in and commonly used feature classes in these two namespaces
1) System.Web.MVC space
AcceptVerbsAttribute,ActionFilterAttribute,ActionNameAttribute,AsyncTimeoutAttribute,AuthorizeAttribute,BindAttribute,
HiddenInputAttribute
ActionFilterAttribute represents the base class of all action-filter properties
HttpDeleteAttribute class, which represents a property that is used to restrict the operation method so that the method only handles HTTP DELETE requests
HttpGetAttribute, an HttpPostAttribute attribute that restricts the operation method to only process PUT requests
HttpPutAttribute represents a property that is used to restrict the operation method so that the method only handles HTTP PUT requests.
Example: ActionNameAttribute specifies the actual actionname of the action, and the usage is as follows
[ActionName()]
public ActionResult Index()
Example: The HiddenInputAttribute class, which represents a property that indicates whether a property value or field value should be rendered as a hidden input element |