I have been struggling with my customization of EditorForModel
and the naming of HTML elements emitted by my code and the built-in MVC3 helpers. My code is very simple, and is clearly missing some subtleties, like naming the the rendered elements properly.
I am looking for advanced resources that can help me hone this area of my current development, especially with a view to subdividing a main view model across smaller sub-models, so that I can apply say three EditorForModel
calls in one view, to split generated model editors across form columns or tab pages.
My current 'override' of the default EditorForModel
template is as follows:
@{
// TODO Filtering for subsets of model without having to bind separate models.
var properties = ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !pm.IsComplexType && !ViewData.TemplateInfo.Visited(pm));
}
<fieldset>
<legend>@ViewData.ModelMetadata.DisplayName</legend>
<ul class="form-column">
@foreach (var prop in properties)
{
<li>
@{
if (prop.HideSurroundingHtml)
{
@Html.Editor(prop.DisplayName ?? prop.PropertyName)
}
else
{
@Html.Label(prop.PropertyName, (prop.IsRequired ? "* " : "") + (prop.DisplayName ?? prop.PropertyName))
@Html.Editor(prop.PropertyName)
}
}
</li>
}
</ul>
</fieldset>
I have copied and modified this code from the Object.ascx example template on this article on Brad Wilson's blog. What resources can I consult to enrich this to cater for as many scenarios as possible, in as rich a manner as possible?
Your template seems pretty good for a very generic editor. If I understand your question properly, you're looking for more ways to break up and filter your model properties.
One way to filter the model into subsets without having to create submodels would be to use attributes. You could create as many attributes as you want, and have them implement IMetadataAware. There you could add arbitrary properties to the ModelMetadata.AdditionalValues
property bag, and have your editor templates inspect those values.
Alternatively you could implement your own custom ModelMetadataProvider
that returns a custom ModelMetadata
object that had whatever properties you wanted.
Either would allow you to simply annotate your model to define filter behavior.
Both these methods are described by who else, Brad Wilson, in this blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With