I am looking for additional resources for customizing MVC3 editor templates

I struggled with my EditorForModel setting and the name of the HTML elements emitted by my code and MVC3 built-in helpers. My code is very simple and explicitly skips some subtleties, for example, correctly naming the displayed elements.

I am looking for advanced resources that can help me hone this area of ​​my current development, especially with the goal of subdividing the main view model into smaller submodels, so that I can apply, say, three calls to EditorForModel in one view, separate the generated model editors through the form columns or tabs.

My current "override" of the EditorForModel template by default 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 copied and modified this code from the Object.ascx example template in this article on Brad Wilson's Blog . What resources can I recommend to enrich this, to satisfy as many possible scenarios as possible?

+7
source share
1 answer

Your template seems pretty good for a very general editor. If I understand your question correctly, you are looking for more ways to break down and filter your model properties.

One way to filter a model into subsets without creating submodels is to use attributes. You can create as many attributes as you like, and it will implement IMetadataAware . There you can add custom properties to the ModelMetadata.AdditionalValues property ModelMetadata.AdditionalValues , and your editor templates will check these values.

Alternatively, you can implement your own ModelMetadataProvider , which returns a custom ModelMetadata object that would have any properties that you would like.

Or you could simply annotate your model to determine the behavior of the filter.

Both of these methods are described by someone else, Brad Wilson, in this post.

+1
source

All Articles