开发者

How does @Html.DisplayForModel() know the underlying data model?

开发者 https://www.devze.com 2023-02-09 04:01 出处:网络
It looks like a magic for me that @Html.DisplayForModel() can render the markup automatically wi开发者_JS百科thout requiring me to explicitly pass the data model to it.

It looks like a magic for me that @Html.DisplayForModel() can render the markup automatically wi开发者_JS百科thout requiring me to explicitly pass the data model to it.

How does @Html.DisplayForModel() know the underlying data model?


I believe it uses @Html.ViewData.ModelMetadata internally to get the necessary information about your model. ModelMetadata has information about all the properties in your model, validation attributes, etc. etc.


Yes, esentially it iterates

<% foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay                         && !ViewData.TemplateInfo.Visited(pm))) { %>

See Brad Wilson's excellent blog http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html

0

精彩评论

暂无评论...
验证码 换一张
取 消