开发者

mvc3 labelfor subobject

i have two classes :

public class foo{
    [Display(Name="H开发者_如何学Goere is my bar")]
    public bar myBar;

    [Display(Name="Here is my name")]
    public string myName;

}

public class bar{
    ...
}

I have a view "chooseBar.cshtml" strongly typed of foo.

If I put a @Html.LabelFor(model => model.myName) -> I see "Here is my name" in the label. But, if i put a @Html.LabelFor(model => model.myBar) -> I see only "myBar", not the expected "Here is my bar"

How can I use the LabelFor for my subobject ?(i.e displaying "Here is my bar" instead of "myBar" in the label)

Thanks for your answer.


You could decorate Bar's property you want to render with the attribute:

public class Bar
{
   [Display(Name="Here is my bar")]
   public string BarName { get; set; }
}

And use a custom editor template:

Bar.cshtml

@Html.LabelFor(model => model.BarName)

chooseBar.cshtml

@Html.EditorFor(model => model.Bar)

Could be another way, but i generally always use editor/display templates when i want to render a sub-model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜