In MVC2, The DescriptionAttribute is not implemented by default (although this attribute can be found, the implementation method is not found by reading the MVC Source Code). This is inconvenient, especially when we use EditorForModel, we need to briefly introduce the field. We will extend this attribute below.
New Class DescriptionMetadataProvider
Then rewrite the CreateMetadata method of DataAnnotationsModelMetadataProvider:
Public class DescriptionMetadataProvider: DataAnnotationsModelMetadataProvider
{
Protected override ModelMetadata CreateMetadata (IEnumerable <System. Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
{
List <System. Attribute> attributeList = new List <System. Attribute> (attributes );
DataAnnotationsModelMetadata result = (DataAnnotationsModelMetadata) base. CreateMetadata (attributes, containerType, modelAccessor, modelType, propertyName );
DescriptionAttribute descriptionAttribute = attributeList. OfType <DescriptionAttribute> (). FirstOrDefault ();
If (descriptionAttribute! = Null)
{
Result. Description = descriptionAttribute. Description;
}
Return result;
}
}
Register DescriptionMetadataProvider in Application_Start () of Global. asax.
ModelMetadataProviders. Current = new DescriptionMetadataProvider ();
Then you can use the following annotations:
[DisplayName ("office address")]
[Required]
[Description ("Enter your real office address")]
Public string OfficeAddress {get; set ;}
To see the actual results:
MVC2 provides us with a good template mechanism and provides you with a learning address:
Http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
By extending this method above, we can design the template in this way.
Create the EditorTemplates folder in the Shared directory and add the MVC user control Object. ascx, please note that when we use <% = Html. when the EditorForModel () %> method is used, MVC automatically searches for the Object. ascx in the order
Views => ControllerName => EditorTemplates => Object. ascx
If the Object. ascx cannot be found in the View of the corresponding Controller, it will continue to be found in the Shared => EditorTemplates folder (the corresponding template aspx will also be found ).
Let's take a look at a simple Object. ascx
Code
<% @ Control Language = "C #" Inherits = "System. Web. Mvc. ViewUserControl" %>
<% If (ViewData. TemplateInfo. TemplateDepth> 1) {%>
<% = ViewData. ModelMetadata. SimpleDisplayText %>
<%}
Else {%>
<Table class = "grid"> <thead> <tr> <th> name </th> <th> content </th> <th> Information </th> </tr> </thead> <tbody>
<% Int I = 0;
Foreach (var prop in ViewData. ModelMetadata. Properties. Where (pm => pm. ShowForEdit &&! ViewData. TemplateInfo. Visited (pm )))
{
%>
<% If (prop. HideSurroundingHtml)
{%>
<% = Html. Editor (prop. PropertyName) %>
<%} Else {%>
<Tr <% = I % 2! = 0? "Class = 'trre'": "" % >>< td style = "width: 100px;" ><% if (! String. isNullOrEmpty (prop. displayName) {%> <% = prop. displayName %> <% }%> </td> <td style = "text-align: left;"> <% = Html. editor (prop. propertyName) %> </td> <% = prop. isRequired? "*": Null %> <% = Html. ValidationMessage (prop. PropertyName) %> <% = prop. Description %> </td> </tr>
<% }%>
<% I ++;
}%> <Tr> <td colspan = "3"> <input type = "submit" value = "submit"/> </td> </tr>
</Tbody> </table>
<% }%>
With the definition of this template, you can try to use the <% = Html. EditorForModel () %> method to see if the effect is different?