How does @Html.DisplayForModel() know the underlying data model?
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
精彩评论