I overridden the default editor template (Object.ascx) to create the HTML code for the form (body) following the Twitter Bootstrap recommendations ( http://twitter.imtqy.com/bootstrap/base-css.html#forms ), which runs via Html.EditorForModel ().
@if (ViewData.TemplateInfo.TemplateDepth > 1) { @(Model == null ? ViewData.ModelMetadata.NullDisplayText : ViewData.ModelMetadata.SimpleDisplayText) } else { foreach (var property in ViewData.ModelMetadata.Properties.Where(m => m.ShowForEdit @*&& m.ModelType != typeof (System.Data.EntityState)*@ && !m.IsComplexType && !ViewData.TemplateInfo.Visited(m))) { if (property.HideSurroundingHtml) { @Html.Editor(property.PropertyName) } else { <div class="control-group @(!ViewData.ModelState.IsValidField(property.PropertyName) ? "error" : ViewData.ModelState.ContainsKey(property.PropertyName) ? "success" : "" )"> @if (!string.IsNullOrEmpty(Html.Label(property.PropertyName).ToHtmlString())) { @Html.Label(property.GetDisplayName() + (property.IsRequired ? " *" : ""), new { @class = "control-label" }) } <div class="controls"> @Html.Editor(property.PropertyName) @Html.ValidationMessage(property.PropertyName, "*", new { @class = "help-inline" }) </div> </div> } } }
This works well if I do not want to change the form (for example, reorder or add fields). I basically want to separate some of the above code in another editor template, resulting in Property.ascx. This will give a label, input, and validation message for the given property, executed via Html.Editor ("{PropertyName"}) or Html.EditorFor (m => m. {PropertyName}).
<div class="control-group @(!ViewData.ModelState.IsValidField(ViewData.ModelMetadata.PropertyName) ? "error" : ViewData.ModelState.ContainsKey(ViewData.ModelMetadata.PropertyName) ? "success" : "" )"> @if (!string.IsNullOrEmpty(Html.Label(ViewData.ModelMetadata.PropertyName).ToHtmlString())) { @Html.Label(ViewData.ModelMetadata.GetDisplayName() + (ViewData.ModelMetadata.IsRequired ? " *" : ""), new { @class = "control-label" }) } <div class="controls"> @Html.Editor(ViewData.ModelMetadata.PropertyName) @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName, "*", new { @class = "help-inline" }) </div> </div>
The problem with the above editor template is that it does not display the correct input for this type. Thus, the date / time property will simply display a normal text field without the type attribute.
Am I going to do it right? Should I use the standard HTML helper or partial view? If so, how do we deal with the general nature?
source share