Reputation: 3363
So, I have a inheritance hierarchy in my Entity Model as bellow:
class Member
{
public virtual Information Information { get; set; }
public long InformationId { get; set; }
//Other Props
}
class Staff : Member
{
}
class Guest : Member
{
}
class Information
{
public string Name { get; set; }
}
class StaffInformation : Information
{
public DateTime BirthDate { get; set; }
}
class GuestInformation : Information
{
public DateTime Expiry { get; set; }
}
In view, I've trouble to cast a Member Information into correct child. For example, I want to:
TextBoxFor(model => model.Staff.StaffInformation.BirthDate)
but what I can do is:
TextBoxFor(model => (Entities.StaffInformation)(model.Staff.Information).BirthDate)
Can I specify the type of Information in a Child? some thing like following pseudo:
class Staff : Member
{
public StaffInformation Information { get; set; }
}
class Guest : Member
{
public GuestInformation Information { get; set; }
}
Any suggestion?
Upvotes: 1
Views: 62
Reputation: 65079
This is a prime candidate for ViewModels.
Your entity model and your view models do not have to be one-to-one. That is the job of a controller (service class, controller, whatever) to do. Convert from one to the other.
It seems silly to upcast a BirthDate on one view.. then upcast an Expiry date on another. This is exactly what a ViewModel is for.
Upvotes: 1