【发布时间】:2021-07-04 04:17:37
【问题描述】:
我正在创建一个系统,其中列出了所有数据员工及其电子邮件和分机等。我通过搜索可以正常工作。
型号:
namespace ServiceDirectory.Models
{
[Table("Employee")]
public class Employee
{
[Key]
public int EmployeeID { get; set; }
[DisplayName("First Name")]
public String Forename { get; set; }
[DisplayName("Surname")]
public String Surname { get; set; }
public String Ext { get; set; }
public String Email { get; set; }
public bool Active { get; set; }
}
}
查看:
@model IEnumerable<ServiceDirectory.Models.Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm("Index", "Employee", FormMethod.Get))
{
<b>Search By:</b> @Html.RadioButton("searchBy", "Surname", true) <text>Surname</text>
@Html.RadioButton("searchBy", "Forename") <text>ForeName</text><br />
@Html.TextBox("search") <input type="submit" value="Search" />
}
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Ext</th>
<th scope="col">Job Title & Location</th>
<th scope="col">Email</th>
</tr>
</thead>
@foreach (var Employee in Model)
{
<tr>
<td>@Employee.Forename @Employee.Surname</td>
<td>@Employee.Ext</td>
<td>Job Title here</td>
<td>@Employee.Email</td>
</tr>
}
</table>
控制器:
namespace ServiceDirectory.Controllers
{
public class EmployeeController : Controller
{
private ApplicationDbContext db;
public EmployeeController()
{
db = new ApplicationDbContext();
}
// GET: Employee
public ActionResult Index(string searchBy, string search)
{
if (searchBy == "Forename")
{
return View(db.Employee.Where(x => x.Forename.StartsWith(search)).Where(e => e.Active).ToList());
}
else
{
return View(db.Employee.Where(x => x.Surname.StartsWith(search)).Where(e => e.Active).ToList());
}
}
protected override void Dispose(bool disposing)
{
db.Dispose();
}
}
}
在我看来,您将在此处看到“职位名称”,我想要为每个员工显示职位名称,我的问题是它在另一个名为 EmployeeDetails 的表中。
如何链接到EmployeeDetails 以获取职位并将其链接到我的员工视图?
任何帮助将不胜感激,这是我的第一个项目 :)
【问题讨论】:
-
能否请您也显示 EmloyeeDetails 课程?
标签: c# sql asp.net-mvc