I've created a class called Employee. An Employee has a IManager object as a property like this:
public class Employee : IManagable{
(...)
public IManager ManagedBy
{get; set;}
(...)
}
This is my Manager class that implements the IManager interface
public class Manager : IManager, IManagable
{
(...)
public string FirstName
{get; set}
public string LastName
{get; set}
(...)
}
Now I want to list a bunch of information with the help of the View code below.
@model IEnumerable<LibraryApplication.Models.Domain.Employee.Employee>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Salary)
</th>
<td>
@Html.DisplayNameFor(model => model.ManagedBy)
</td>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Salary)
</td>
<td>
@Html.DisplayFor(modelItem => item.ManagedBy)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
If I want to access an attribute of the manager, what can I do? I've tried this: @Html.DisplayFor(modelItem => item.ManagedBy.FirstName)
to add the first name of the manager in the table but it throws this error
System.ArgumentException: 'The property LibraryApplication.Models.Domain.Employee.IManager.FirstName could not be found
When i just do this @Html.DisplayFor(modelItem => item.ManagedBy)
it lists all the properties for the manager like below, which is not relevant in this case.
what can i do?
The IManager interface is this simple interface:
public interface IManager : IEmployee
{
}
(it will declare other methods in the future)
and this is my IEmployee interface:
namespace LibraryApplication.Models.Domain.Employee
{
public interface IEmployee
{
int Id
{
get;
set;
}
string FirstName
{
get;
set;
}
string LastName
{
get;
set;
}
double Salary
{
get;
set;
}
bool IsCEO
{
get;
}
bool IsManager
{
get;
}
}
}
As you can see, the Manager has a FirstName property so I can easily reference it. But why can't i do it in my View file?
EDIT
Now I can make it work in 2 different ways. Either I change the ManagedBy
property to Manager
instead of IManager
. OR i add the following to my IManager
interface:
public IManager ManagedBy
{get; set;}
It works both ways. But why do i have to declare ManagedBy
in IManager
as well if I already declare it in the IEmployee
interface that IManager
implements?
you should use manager mangedby instead of Imanager
public class Employee : IManagable{
(...)
public Manager ManagedBy
{get; set;}
(...)
}
you have to review your logic:
manager is also an employee, so you could derive Manager from IEmployee
you specify in Interface IManager, what is just needed for managers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.