首页 > 代码库 > asp.net MVC 4.0 Model元数据回顾——HtmlHelper的ModelMetadata

asp.net MVC 4.0 Model元数据回顾——HtmlHelper的ModelMetadata

模板方法包括Display/DisplayFor、Editor/EditorFor、DisplayForModel/EditForModel提供辅助生成Html的模型元数据信息

1 public static MvcHtmlString EditorForModel(this HtmlHelper html, string templateName, string htmlFieldName, object additionalViewData)2 {3    return MvcHtmlString.Create(TemplateHelpers.TemplateHelper(html, html.ViewData.ModelMetadata, htmlFieldName, templateName, DataBoundControlMode.Edit, additionalViewData));4 }

获取模型元数据

 1 public virtual ModelMetadata ModelMetadata 2 { 3     get 4     { 5         if (this._modelMetadata =http://www.mamicode.com/= null && this._model != null) 6         { 7             this._modelMetadata = http://www.mamicode.com/ModelMetadataProviders.Current.GetMetadataForType(() => this._model, this._model.GetType()); 8         } 9         return this._modelMetadata;10     }11     set12     {13         this._modelMetadata =http://www.mamicode.com/ value;14     }15 }

asp.net MVC 4.0 Model元数据回顾——HtmlHelper的ModelMetadata