This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19676|Reply: 2

[Tips] MVC metamodel metadata ViewData.ModelMetadata

[Copy link]
Posted on 6/28/2017 9:12:49 PM | | | |

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




Previous:[A] MySql.Data.MySqlClient.MySqlConnection cannot be cast to [B]MySql.Data.MySqlC...
Next:Uncaught TypeError: Cannot set property 'unobtrusive' of undefined
 Landlord| Posted on 6/28/2017 9:14:05 PM |
ModelMetadata



 Landlord| Posted on 12/1/2017 4:53:12 PM |


ViewData to get the property code:

ViewDataDictionary viewData2=new ViewDataDictionary(ViewData["Test"]);
viewData2.ModelMetadata.Properties;
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com