Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditorForModel - Duplicate labels when using editorfor

I am using a number of editor templates for different datatypes (string, DateTime, etc.) in my ASP.NET MVC application. For instance, the following is what I am using for strings:

<div class="editor-label">
     @Html.Label("")
</div>
<div class="editor-field">
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @class = "text", placeholder = ViewData.ModelMetadata.Watermark })
    @Html.ValidationMessage("")
</div>

I would like to use EditorForModel within my views but when I do so it appears to add it's own label for each property resulting in duplicate labels (because I have a label in my string editor template). Is there any way, other than removing the label from my string editortemplate (in this example) that I can tell editorformodel not to insert a label?

like image 687
JP. Avatar asked Feb 20 '23 11:02

JP.


1 Answers

One possibility is to override the default object editor template (~/Views/Shared/EditorTemplates/Object.cshtml) and remove the label it adds:

@if (ViewData.TemplateInfo.TemplateDepth > 1) 
{
    @ViewData.ModelMetadata.SimpleDisplayText
}
else 
{
    foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) 
    {
        if (prop.HideSurroundingHtml) 
        {
            @Html.Editor(prop.PropertyName)
        }
        else 
        {
            <div class="editor-field">
                @Html.Editor(prop.PropertyName)
                @Html.ValidationMessage(prop.PropertyName)
            </div>
        }
    }    
}
like image 116
Darin Dimitrov Avatar answered Mar 03 '23 05:03

Darin Dimitrov