Reputation: 920
I'm setting up a bunch of Razor pages for maintaining reference tables. The basic structure in the data context is this:
public class RefTableBase {
public int Id {get;set;}
public string Value {get;set;}
}
public class UserType: RefTableBase {}
public class ReferenceType: RefTableBase {}
The scaffolded Razor pages work correctly there. However, When I have a class that calls the derrived tables, the pages don't display what I expect.
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;}
}
When the index.cshtml page is scaffolded, the tables headers look like this:
@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>
But when the page is actually rendered in the browser, the column headings show
Id Name Value Value
When what I want is:
Id Name User Type Reference Type
I've tried using DisplayAttribute on the members within the class, but it didn't work.
public class SomethingImportant {
// ...........
[Display(Name="User Type")]
public int UserTypeId {get;set;}
public virtual UserType UserType {get;set;}
// ...........
}
Outside of skipping the inheritence and actually setting the DisplayAttribute for each and every class in the derived classes, is there any way I can get it to show how I want?
Upvotes: 0
Views: 252
Reputation: 4168
You can simply put the Display attribute on the properties in question:
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;}
}
And remove calling .Value
on the view.
<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>
If you need .Value
and it's a property of that class, you can put the Display
attribute on those properties instead.
Upvotes: 2