Skip to content

Instantly share code, notes, and snippets.

@hazzik
Created September 7, 2011 14:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hazzik/1200683 to your computer and use it in GitHub Desktop.
Save hazzik/1200683 to your computer and use it in GitHub Desktop.
@{
Func<ModelMetadata, bool> ShouldShow = metadata =>
metadata.ShowForEdit && !ViewData.TemplateInfo.Visited(metadata);
}
@if (ViewData.TemplateInfo.TemplateDepth > 5) {
if (Model == null) {
@ViewData.ModelMetadata.NullDisplayText
} else {
@ViewData.ModelMetadata.SimpleDisplayText
}
} else {
foreach (var prop in ViewData.ModelMetadata.Properties.Where(ShouldShow)) {
if (prop.HideSurroundingHtml) {
@Html.Editor(prop.PropertyName)
} else {
if (string.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())==false) {
<div class="editor-label">
@Html.Label(prop.PropertyName)
</div>
}
<div class="editor-field">
@Html.Editor(prop.PropertyName)
@Html.ValidationMessage(prop.PropertyName)
</div>
}
}
}
@sampalmer
Copy link

This seems to introduce a bug in which viewmodel properties whose names match ViewBag or ViewData properties are overridden. For example, a property called Title will be overriden by the page title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment