Hide editor-label for public property when calling EditorFor(...)?
Asked Answered
T

1

7

When calling Html.EditorFor(m => m), where m is a public class with public properties, a hidden input and a label are displayed for properties with the [HiddenInput] attribute.

  • How can I hide the label without making it private or creating an editor template?

Example

public class User
{
    [HiddenInput]
    public Guid ID { get; set; } // should not be displayed in editor template
    public string Name { get; set; } // should be editable
}

Undesired result for ID property by EditorFor(...) with label

<div class="editor-label">
    <label for="ID">ID</label> <!-- Why is this here? -->
</div>
<div class="editor-field">
    <input id="ID" name="ID" type="hidden" value="">
</div>
Triboluminescence answered 8/5, 2010 at 9:36 Comment(2)
I tried [ScaffoldColumn(false)], which hides the input as well, but I want to keep the hidden input and just get rid of the label.Triboluminescence
Tried [DisplayName("")], which hides the <label> tag, but not the surrounding <div class="editor-label"> with "*" required indicator inside.Triboluminescence
T
10

Solved with:

[HiddenInput(DisplayValue=false)]

Otherwise HideSurroundingHtml is not set correctly.

Triboluminescence answered 8/5, 2010 at 10:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.