我在名为的视图中有以下 cshtml 代码ViewDepartment.cshtml
@model DepartmentViewModel
<head>
<meta charset="iso-8859-1">
</head>
<body>
<div class="d-flex flex-row">
<a class="navbar-text">@Html.DisplayNameFor(o => o.HeadOfTheDepartment): </a>
<a class="nav-link" asp-action="Profile" asp-controller="Faculty" asp-route-Id="@Model.HeadOfTheDepartment.FacultyId">@Html.DisplayFor(o => o.HeadOfTheDepartment.FacultyName)</a>
</div>
@*Professors*@
<div class="d-flex flex-column">
<a class="navbar-text align-content-center">@Html.DisplayNameFor(o => o.Professors): </a>
<table class="table table-bordered">
<thead>
<tr class="d-table-row">
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyName)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyDepartment)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyDesignation)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyAddress)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyEmail)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyPhoneNumber)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyStatus)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (Faculty faculty in Model.Professors)
{
<tr class="d-table-row">
<td>@Html.DisplayFor(o => faculty.FacultyName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDepartment.DepartmentName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDesignation)</td>
<td>@Html.DisplayFor(o => faculty.FacultyAddress)</td>
<td>@Html.DisplayFor(o => faculty.FacultyEmail)</td>
<td>@Html.DisplayFor(o => faculty.FacultyPhoneNumber)</td>
<td>@Html.DisplayFor(o => faculty.FacultyStatus)</td>
<td><a class="nav-link" asp-action="Profile" asp-controller="Faculty" asp-route-Id="@faculty.FacultyId">View Profile</a></td>
</tr>
}
</tbody>
</table>
</div>
@*Associate Professors*@
<div class="d-flex flex-column">
<a class="navbar-text align-content-center">@Html.DisplayNameFor(o => o.AssociateProfessors): </a>
<table class="table table-bordered">
<thead>
<tr class="d-table-row">
<th>@Html.DisplayNameFor(o => o.AssociateProfessors.First().FacultyName)</th>
<th>@Html.DisplayNameFor(o => o.AssociateProfessors.First().FacultyDepartment)</th>
<th>@Html.DisplayNameFor(o => o.AssociateProfessors.First().FacultyDesignation)</th>
<th>@Html.DisplayNameFor(o => o.AssociateProfessors.First().FacultyAddress)</th>
<th>@Html.DisplayNameFor(o => o.AssociateProfessors.First().FacultyEmail)</th>
<th>@Html.DisplayNameFor(o => o.AssociateProfessors.First().FacultyPhoneNumber)</th>
<th>@Html.DisplayNameFor(o => o.AssociateProfessors.First().FacultyStatus)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var faculty in Model.AssociateProfessors)
{
<tr class="d-table-row">
<td>@Html.DisplayFor(o => faculty.FacultyName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDepartment.DepartmentName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDesignation)</td>
<td>@Html.DisplayFor(o => faculty.FacultyAddress)</td>
<td>@Html.DisplayFor(o => faculty.FacultyEmail)</td>
<td>@Html.DisplayFor(o => faculty.FacultyPhoneNumber)</td>
<td>@Html.DisplayFor(o => faculty.FacultyStatus)</td>
<td><a class="nav-link" asp-action="Profile" asp-controller="Faculty" asp-route-Id="@faculty.FacultyId">View Profile</a></td>
</tr>
}
</tbody>
</table>
</div>
@*Assistant Professors*@
<div class="d-flex flex-column">
<a class="navbar-text align-content-center">@Html.DisplayNameFor(o => o.AssistantProfessors): </a>
<table class="table table-bordered">
<thead>
<tr class="d-table-row">
<th>@Html.DisplayNameFor(o => o.AssistantProfessors.First().FacultyName)</th>
<th>@Html.DisplayNameFor(o => o.AssistantProfessors.First().FacultyDepartment)</th>
<th>@Html.DisplayNameFor(o => o.AssistantProfessors.First().FacultyDesignation)</th>
<th>@Html.DisplayNameFor(o => o.AssistantProfessors.First().FacultyAddress)</th>
<th>@Html.DisplayNameFor(o => o.AssistantProfessors.First().FacultyEmail)</th>
<th>@Html.DisplayNameFor(o => o.AssistantProfessors.First().FacultyPhoneNumber)</th>
<th>@Html.DisplayNameFor(o => o.AssistantProfessors.First().FacultyStatus)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var faculty in Model.AssistantProfessors)
{
<tr class="d-table-row">
<td>@Html.DisplayFor(o => faculty.FacultyName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDepartment.DepartmentName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDesignation)</td>
<td>@Html.DisplayFor(o => faculty.FacultyAddress)</td>
<td>@Html.DisplayFor(o => faculty.FacultyEmail)</td>
<td>@Html.DisplayFor(o => faculty.FacultyPhoneNumber)</td>
<td>@Html.DisplayFor(o => faculty.FacultyStatus)</td>
<td><a class="nav-link" asp-action="Profile" asp-controller="Faculty" asp-route-Id="@faculty.FacultyId">View Profile</a></td>
</tr>
}
</tbody>
</table>
</div>
@*Lecturers*@
<div class="d-flex flex-column">
<a class="navbar-text align-content-center">@Html.DisplayNameFor(o => o.Lecturers): </a>
<table class="table table-bordered">
<thead>
<tr class="d-table-row">
<th>@Html.DisplayNameFor(o => o.Lecturers.First().FacultyName)</th>
<th>@Html.DisplayNameFor(o => o.Lecturers.First().FacultyDepartment)</th>
<th>@Html.DisplayNameFor(o => o.Lecturers.First().FacultyDesignation)</th>
<th>@Html.DisplayNameFor(o => o.Lecturers.First().FacultyAddress)</th>
<th>@Html.DisplayNameFor(o => o.Lecturers.First().FacultyEmail)</th>
<th>@Html.DisplayNameFor(o => o.Lecturers.First().FacultyPhoneNumber)</th>
<th>@Html.DisplayNameFor(o => o.Lecturers.First().FacultyStatus)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var faculty in Model.Lecturers)
{
<tr class="d-table-row">
<td>@Html.DisplayFor(o => faculty.FacultyName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDepartment.DepartmentName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDesignation)</td>
<td>@Html.DisplayFor(o => faculty.FacultyAddress)</td>
<td>@Html.DisplayFor(o => faculty.FacultyEmail)</td>
<td>@Html.DisplayFor(o => faculty.FacultyPhoneNumber)</td>
<td>@Html.DisplayFor(o => faculty.FacultyStatus)</td>
<td><a class="nav-link" asp-action="Profile" asp-controller="Faculty" asp-route-Id="@faculty.FacultyId">View Profile</a></td>
</tr>
}
</tbody>
</table>
</div>
</body>
可以看出,对于4个不同的名称,即Professor、AssociateProfessor、AssistantProfessor和Lecturer,它们都是Designation
枚举类型:
public Enum Designation
{
Professor,
AssociateProfessor,
AssistantProfessor,
Lecturer
}
我必须编写相同的代码框架,即,
@*Professors*@
<div class="d-flex flex-column">
<a class="navbar-text align-content-center">@Html.DisplayNameFor(o => o.Professors): </a>
<table class="table table-bordered">
<thead>
<tr class="d-table-row">
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyName)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyDepartment)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyDesignation)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyAddress)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyEmail)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyPhoneNumber)</th>
<th>@Html.DisplayNameFor(o => o.Professors.First().FacultyStatus)</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (Faculty faculty in Model.Professors)
{
<tr class="d-table-row">
<td>@Html.DisplayFor(o => faculty.FacultyName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDepartment.DepartmentName)</td>
<td>@Html.DisplayFor(o => faculty.FacultyDesignation)</td>
<td>@Html.DisplayFor(o => faculty.FacultyAddress)</td>
<td>@Html.DisplayFor(o => faculty.FacultyEmail)</td>
<td>@Html.DisplayFor(o => faculty.FacultyPhoneNumber)</td>
<td>@Html.DisplayFor(o => faculty.FacultyStatus)</td>
<td><a class="nav-link" asp-action="Profile" asp-controller="Faculty" asp-route-Id="@faculty.FacultyId">View Profile</a></td>
</tr>
}
</tbody>
</table>
</div>
4次!(当然是改变Professors
toLecturers
之类的)
这似乎是多余的,我想知道是否有一种方法可以让我只编写一次框架,该框架将适用于所有 4 种指定类型。这样,即使Designations
以后添加更多内容,它也会自动包含在当前代码中。
Professors
(及其姊妹属性)属于 type IList<Faculty>
,其中Faculty
class 如下:
public class Faculty
{
[Required]
public string FacultyId { get; set; }
[Required]
public string FacultyName { get; set; }
[Required]
public FacultyStatus FacultyStatus { get; set; }
[Required]
public Designation FacultyDesignation { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string FacultyEmail { get; set; }
[Required]
[DataType(DataType.PhoneNumber)]
public string FacultyPhoneNumber { get; set; }
[Required]
public string FacultyAddress { get; set; }
[Required]
public int FacultyExperience { get; set; }
[Required]
public string FacultyQualifications { get; set; }
[Required]
public string FacultyDescription { get; set; }
public Department HODDepartment { get; set; }
[ForeignKey("FacultyDepartment")]
public string FacultyDepartmentId { get; set; }
public Department FacultyDepartment{ get; set; }
}
我尝试了一种使用反射的方法(在帖子的先前版本中可用),但不太成功。我猜,将需要某些版本的 ForEach 循环,它将循环Enum.GetNames(typeof(Designation)).Cast<Designation>()
. 但是,我无法弄清楚我需要的反射构造,因为我对反射的概念还很陌生。我需要有人指出我正确的方向。