C# 使用继承时的MVC/Razor显示属性

C# 使用继承时的MVC/Razor显示属性,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我正在设置一堆剃须刀页面来维护参考表。数据上下文中的基本结构如下: public class RefTableBase { public int Id {get;set;} public string Value {get;set;} } public class UserType: RefTableBase {} public class ReferenceType: RefTableBase {} 脚手架的剃须刀页面在那里正常工作。但是,当我有一个调用驱动表的类时,页面

我正在设置一堆剃须刀页面来维护参考表。数据上下文中的基本结构如下:

public class RefTableBase {
    public int Id {get;set;}
    public string Value {get;set;}
}

public class UserType: RefTableBase {}

public class ReferenceType: RefTableBase {}
脚手架的剃须刀页面在那里正常工作。但是,当我有一个调用驱动表的类时,页面不会显示我所期望的内容

public class SomethingImportant {
    public int Id {get;set;}
    public string Name {get;set;}

    public int UserTypeId {get;set;}
    public virtual UserType UserType {get;set;}

    public int ReferenceTypeId {get;set;}
    public virtual ReferenceType ReferenceType {get;set;}
}
当index.cshtml页面被搭建时,表标题如下所示:

@model IEnumerable<Models.SomethingImportant>

<table class="table">
    <tr>
        <th>@Html.DisplayNameFor(model => model.Id)</th>
        <th>@Html.DisplayNameFor(model => model.Name)</th>
        <th>@Html.DisplayNameFor(model => model.UserType.Value)</th>
        <th>@Html.DisplayNameFor(model => model.ReferenceType.Value)</th>
当我想要的是:

Id    Name    User Type     Reference Type
我尝试对类中的成员使用DisplayAttribute,但没有成功

public class SomethingImportant {
    // ...........
    [Display(Name="User Type")]
    public int UserTypeId {get;set;}
    public virtual UserType UserType {get;set;}
    // ...........
}

除了跳过继承并为派生类中的每个类实际设置DisplayAttribute之外,是否有任何方法可以让它显示我想要的内容?

您只需将Display属性放在相关属性上:

public class SomethingImportant {
    public int Id {get;set;}
    public string Name {get;set;}

    public int UserTypeId {get;set;}
    [Display(Name="User Type")]//here
    public virtual UserType UserType {get;set;}

    public int ReferenceTypeId {get;set;}
    [Display(Name="Reference Type")]//and here
    public virtual ReferenceType ReferenceType {get;set;}
}
并删除视图上的调用
.Value

<th>@Html.DisplayNameFor(model => model.Id)</th>
<th>@Html.DisplayNameFor(model => model.Name)</th>
<th>@Html.DisplayNameFor(model => model.UserType)</th>
<th>@Html.DisplayNameFor(model => model.ReferenceType)</th>
@Html.DisplayNameFor(model=>model.Id)
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.UserType)
@DisplayNameFor(model=>model.ReferenceType)

如果您需要
.Value
并且它是该类的属性,您可以将
Display
属性放在这些属性上。

您只需将Display属性放在相关属性上:

public class SomethingImportant {
    public int Id {get;set;}
    public string Name {get;set;}

    public int UserTypeId {get;set;}
    [Display(Name="User Type")]//here
    public virtual UserType UserType {get;set;}

    public int ReferenceTypeId {get;set;}
    [Display(Name="Reference Type")]//and here
    public virtual ReferenceType ReferenceType {get;set;}
}
并删除视图上的调用
.Value

<th>@Html.DisplayNameFor(model => model.Id)</th>
<th>@Html.DisplayNameFor(model => model.Name)</th>
<th>@Html.DisplayNameFor(model => model.UserType)</th>
<th>@Html.DisplayNameFor(model => model.ReferenceType)</th>
@Html.DisplayNameFor(model=>model.Id)
@DisplayNameFor(model=>model.Name)
@DisplayNameFor(model=>model.UserType)
@DisplayNameFor(model=>model.ReferenceType)

如果您需要
.Value
并且它是该类的一个属性,您可以将
Display
属性放在这些属性上。

我需要是
@Html.DisplayNameFor(model=>model.UserType)
和应用于SomethingImportant中的
UserType
UserType属性的
DisplayAttribute
没有显示属性必须从最后两行中删除.value。(即model.UserType.Value-->model.UserType)我需要是
@Html.DisplayNameFor(model=>model.UserType)
,并且应用于SomethingImportant中的
UserType
UserType属性的
DisplayAttribute
没有DisplayAttribute必须从最后两行中删除.Value。(ie model.UserType.Value-->model.UserType)我尝试的所有不同组合。。。这工作做得很好!非常感谢你!在我尝试过的各种组合中。。。这工作做得很好!非常感谢你!