0

I have a Model with some complex Properties:

public class TestModel
{
    public string Prop1 { get; set; }
    public SubClass Prop2 { get; set; }
}
public class SubClass 
{
    public string Test { get; set; }
}

public class TestModelMetadata : ModelMetadataConfiguration<TestModel>
{
    public TestModelMetadata ()
    {
        Configure(m => m.Prop1).DisplayName("is going to be displayed");
        Configure(m => m.Prop2.Test).DisplayName("is NOT going to be displayed");
    }
}

When i am trying to display the Model on the View:

@Html.LabelFor(m => m.Prop1)
@Html.LabelFor(m => m.Prop2.Test)

the correct Label for Prop1 is displayed, for Prop2.Test not.

does anybody know a solution for that? thanks!!!!!

Gerwald
  • 1,549
  • 2
  • 18
  • 42

1 Answers1

0

Metadata configuration should be created for each type as I know, in your case for SubClass :

public class SubClassMetadata : ModelMetadataConfiguration<SubClass>
{
    public SubClassMetadata()
    {
        Configure(m => m.Test).DisplayName("is going to be displayed1");
    }
}

Also you should override display template for object if you want to display model with subclass properties using @Html.DisplayForModel because default mvc (3 - i don't know if it was changed in v4) display template for object skip "ComplexType" properties. The start point for customizing default template can be https://github.com/ASP-NET-MVC/ASP.NET-Mvc-3/blob/master/mvc3/src/MvcFuturesFiles/DefaultTemplates/DisplayTemplates/Object.ascx

Hope it helps.

Denis Borovnev
  • 486
  • 3
  • 3
  • Thanks, that was it. Unfortunately I would have prefered to configure it in the way I thought it will be correct (`Configure(m => m.Prop2.Test).DisplayName("is ...");`) - because there, I would have been flexible with configuring my `SubClass` different depending in which Model I am using it. – Gerwald Sep 07 '12 at 16:09
  • Regarding the `@Html.DisplayFor`: that was wrong from my side. I meant: `LabelFor` – Gerwald Sep 07 '12 at 16:15