0

How can i hide the attribute:

[Display(Name = "dspName")]

alternatively the variable name from my variable in the (razor) view?

My problem is that I have defined a custom template for booleans that views the boolean like:

varname/displayName: 'box'

If I create the view with:

@Html.EditorForModel(Model)

the Result is:

varname/displayName
varname/displayName: 'box'

Result in Browser:

html output

edit: my BooleanTemplate

@model System.Boolean?

@{
    string name = string.Empty;
    if (!string.IsNullOrWhiteSpace(ViewData.ModelMetadata.DisplayName))
    {
        name =ViewData.ModelMetadata.DisplayName;
    }
    else
    {
        name = ViewData.ModelMetadata.PropertyName;
    }
}
@name: 
@Html.CheckBox("", Model.HasValue ? Model : Model.Value)
jwillmer
  • 3,570
  • 5
  • 37
  • 73

1 Answers1

2

The additional label you are seeing is baked into the default editor template for the Object class. So you have two possibilities:

  1. Use @Html.EditorFor(x => x.SomeBoolProperty) and so on for each property instead of @Html.EditorForModel()
  2. Modify the default editor template of the object class (EditorTemplates/Object.cshtml) to remove the label (notice the part I have put in comments):

    @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 
            {
                @*if (!String.IsNullOrEmpty(Html.Label(prop.PropertyName).ToHtmlString())) 
                {
                    <div class="editor-label">@Html.Label(prop.PropertyName)</div>
                }*@
                <div class="editor-field">
                    @Html.Editor(prop.PropertyName)
                    @Html.ValidationMessage(prop.PropertyName, "*")
                </div>
            }
        }
    }
    
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • +1, good call. It sounds like he meant to go with option #1, based on how he wrote his template. That's actually what I thought he did, overlooking the `Model` part, until you answered. – Jerad Rose Jun 22 '11 at 14:07
  • all displaynames are gone but now my custom boolean-template shows also no displayname - why? – jwillmer Jun 22 '11 at 14:09
  • @Jerad Rose Need to use option #2 because my Model is created dynamicaly ;-) [link](http://stackoverflow.com/questions/6337246/how-to-convert-a-http-request-into-the-right-object) – jwillmer Jun 22 '11 at 14:10
  • @myName, your custom boolean display template uses the `@name` variable which is initialized from the modelmetadata, so normally it should use the value coming from the Display attribute or if none specified use the property name. – Darin Dimitrov Jun 22 '11 at 14:19
  • @Darin Dimitrov Doesn´t understand it, yes i use ModelMetadate.DisplayName/PropertyName but why can i not display it when i use the object-Template with the outcomment lines? – jwillmer Jun 22 '11 at 14:28
  • @myName, I really have hard time understanding you. Could you please try to explain exactly what steps did you do, what templates did you customize, how do they look, what is the expected result and what is the actual result and how do both differ? – Darin Dimitrov Jun 22 '11 at 14:42
  • @Darin Dimitrov I used your object-template and my boolean-template. I understand that no DisplayName will be displayed for each variable type because in the object-template it is out comment but for boolean typ variables i have another template so why doesent display my view the DisplayNames of the booleans? – jwillmer Jun 22 '11 at 14:50
  • @myName, it should display them, inside your boolean template you have `@name: @Html.CheckBox("", Model.HasValue ? Model : Model.Value)` where `@name` is a variable defined in your code. What does it display instead? Don't you get the value of the Display attribute and a checkbox in the rendered HTML? – Darin Dimitrov Jun 22 '11 at 14:54
  • @Darin Dimitrov `ModelMetadata.PropertyName`/`ModelMetadata.DisplayName` are both empty if i use your object-template – jwillmer Jun 22 '11 at 15:05
  • @myName, that's very weird, it wasn't the case when I tested on my computer. – Darin Dimitrov Jun 22 '11 at 15:16
  • @Darin Dimitrov well i will google it and will post the result but the main question is solved so i accept your answer - thx for your time – jwillmer Jun 22 '11 at 15:18