【问题标题】:ASP.NET MVC get data from another table and link togetherASP.NET MVC 从另一个表中获取数据并链接在一起
【发布时间】: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 以获取职位并将其链接到我的员工视图?

任何帮助将不胜感激,这是我的第一个项目 :)

【问题讨论】:

标签: c# sql asp.net-mvc


【解决方案1】:

首先,创建一个如下所示的类(视图模型)。您将列出要显示的属性。我把它命名为 EmpVM

 public class EmpVM
{
    public int EmployeeID { get; set; }

    public String Forename { get; set; }

    public String Surname { get; set; }
    public String Ext { get; set; }
    public String Email { get; set; }
    public bool Active { get; set; }

    public String JobTitle { get; set; }
}

然后在您的控制器中,您使用 Linq join 来加入 Employee 表和 Employee 详细信息,并将结果作为 EmpVM 类推送

  public ActionResult Index(string searchBy, string search)
    {

        List<EmpVM> Employee = new List<EmpVM>();
      


            var Emp = (from E1 in db.Employee
                       join E2 in db.EmployeeDetail
                       on E1.EmployeeID equals E2.EmployeeId
                       select new EmpVM
                       {
                           EmployeeID = E1.EmployeeID,
                           Forename = E1.Forename,
                           Surname = E1.Surname,
                           Ext = E1.Ext,
                           Email = E1.Email,
                           Active = E1.Active, 
                           JobTitle = E2.JobTitle
                       });

        if (searchBy == "Forename")
        {
            Emp.Where(a => a.Forename.StartsWith(search) && a.Active == true);
        }
        else
        {
            Emp.Where(a => a.Surname.StartsWith(search) && a.Active == true);
        }


        Employee = Emp.ToList();

        return View(Employee);

    }

视图将非常简单。您只需要显示集合。它就像(记得替换你自己的命名空间)

@model IEnumerable<Incendo.Web.Models.EmpVM>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>



@*Your search method here*@

<table class="table table-condensed ">

@foreach (var item in Model)
{
    <tr>

        <td>@item.Forename @item.Surname</td>
        <td>@item.Ext</td>
        <td>@item.Email</td>
        <td>@item.JobTitle</td>
    </tr>

  

}
</table>

我没有测试过它,但我认为它应该可以工作。让我知道你的结果。

【讨论】:

  • 太好了,Linq 不是我遇到的东西,所以我必须看看。我现在得到一份清单,上面有职位名称,谢谢。我唯一的问题是搜索似乎不起作用,我只是得到一个长长的列表,包括所有不活动的,有什么想法吗?再次感谢
  • 您是否将 html.BeginForm 部分插入到视图中?尝试在控制器索引中插入断点并检查 searchby 和 search 值。还有一件事,我强烈建议你为此使用分页。
  • @Matt ,嗨,我意识到我错过了过滤器部分的一些代码。我已经创建了测试程序并且它正在工作。同时,请检查编辑后的答案。肯定有效!
  • 感谢您的帮助,我通过添加 return View(Emp.Where(a => a.Forename.StartsWith(search) && a.Active == true));这似乎已经成功了,并停止了开始时出现的长列表。我只需要一些有关 Linq 添加第三张表的帮助。但我会为此创建另一个线程。那是对你的帮助,学习一些东西和让它工作总是好的!谢谢
【解决方案2】:

您可以通过拥有一个视图模型来实现。我假设您的 EmployeeDetail 类如下所示

 public class EmployeeDetail
 {
    public int Id { get; set; }
    public int EmployeeId { get; set; }

    public string JobDescription { get; set; }
  }

为此目的创建一个 ViewModel 并将其命名为 EmployeeVM

 public class EmployeeVM
{
    public List<Employee> Employees { get; set; }
    public List<EmployeeDetail> EmployeeDetails { get; set; }
}

在你的控制器中

  public ActionResult Index(string searchBy, string search)
    {

        List<EmpVM> Employee = new List<EmpVM>();
      


            var Emp = (from E1 in db.Employee
                       join E2 in db.EmployeeDetail
                       on E1.EmployeeID equals E2.EmployeeId
                       select new EmpVM
                       {
                           EmployeeID = E1.EmployeeID,
                           Forename = E1.Forename,
                           Surname = E1.Surname,
                           Ext = E1.Ext,
                           Email = E1.Email,
                           Active = E1.Active, 
                           JobTitle = E2.JobTitle
                       });

        if (searchBy == "Forename")
        {
            if (!String.IsNullOrEmpty(search))
            {
                Emp = Emp.Where(a => a.Forename.StartsWith(search) && a.Active == true);
            }
            
        }
        else
        {
            if (!String.IsNullOrEmpty(search))
            {
                Emp = Emp.Where(a => a.Surname.StartsWith(search) && a.Active == true);
            }
        }


        Employee = Emp.ToList();

        return View(Employee);

    }

在你看来

@model IEnumerable<Incendo.Web.Models.EmpVM>

@{
 ViewBag.Title = "Index";
 }

<h2>Index</h2>



@using (Html.BeginForm())
{
<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 table-condensed ">

@foreach (var item in Model)
{
    <tr>

        <td>@item.Forename @item.Surname</td>
        <td>@item.Ext</td>
        <td>@item.Email</td>
        <td>@item.JobTitle</td>
    </tr>



}
</table>

【讨论】:

  • 感谢您的回复,我的视图有问题,它无法识别模型 Inendo,因为我的视图中已有模型无法使用 2?
  • 你需要根据你的项目命名空间来改变它
  • 非常感谢,甚至在途中学到了一些东西! 1 个问题,我如何让 JOB 进入每个循环的第一个?这可能吗?
  • 第一个循环来自生成项目的员工(指模型中的每个员工)。第二个循环是根据第一个循环项进行循环,该循环项产生 item1,该 item1 引用引用该项目的员工详细信息。 Model.EmployeeDetails.Where(a => a.EmployeeId == item.EmployeeID) 发挥作用的地方。
  • 感谢您的回复,我想我正在寻找的是有可能拥有这样的@item.Forename @item.Surname @item.Ext @item1.JobDescription @item.Email Item 和 Item1 在同一个 foreach 循环中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多