MVC ASP.NET 不显示使用 join 的数据库的任何结果

问题描述 投票:0回答:2

我已经创建了一个 MVC 应用程序,它应该使用 LINQ 连接数据库中的两个表;应用程序运行时没有显示结果。此外,我在浏览器中查看了 inspect 元素,但没有显示 foreach 循环中的表格?

ViewModel.cs

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;

   namespace WorkingWithMultipleDataTables.Models
   {
       public class ViewModel
       {
           public Employee employee { get; set; }
           public Company companies { get; set; }
       }
   }

HomeController.cs

   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Web;
   using System.Web.Mvc;
   using WorkingWithMultipleDataTables.Models;

   namespace WorkingWithMultipleDataTables.Controllers
   {
       public class HomeController : Controller
       {
           // GET: Home
           public ActionResult Index()
           {
               using (Netmatters_reflection_dbEntities db = new Netmatters_reflection_dbEntities())
               {

                   List<Employee> employees = db.Employees.ToList();
                   List<Company> companies = db.Companies.ToList();
              
                   var employeeRecord = from e in employees
                                        join d in companies on e.EmployeeId equals d.CompanyId
                                        select new ViewModel
                                        {
                                            employee = e,
                                            companies = d,
                                        };
                   return View(employeeRecord);
                }
           }

       }
   }

Index.cshtml

   @model IEnumerable<WorkingWithMultipleDataTables.Models.ViewModel>
   @{
       Layout = null;
    }

        <table class="table table-striped table-bordered">
            <thead class="bg-dark text-white">
                <tr>
                    <th>EmployeeId</th>
                    <th>First_Name</th>
                    <th>Last_Name</th>
                    <th>Company</th>
                    <th>Email</th>
                    <th>Phone</th>

                    <th>CompanyId</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Logo</th>
                    <th>Website</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>@item.employee.EmployeeId</td>
                        <td>@item.employee.First_Name</td>
                        <td>@item.employee.Last_Name</td>
                        <td>@item.employee.Company</td>
                        <td>@item.employee.Email</td>
                        <td>@item.employee.Phone</td>

                        <td>@item.companies.CompanyId</td>
                        <td>@item.companies.Name</td>
                        <td>@item.companies.Email</td>
                        <td>@item.companies.Logo</td>
                        <td>@item.companies.Website</td>


                    </tr>

                }
            </tbody>
        </table>

我已经调试了应用程序,但没有发现问题;当我构建应用程序时,它显示构建成功 ========== 构建:1 成功,0 失败,0 最新,0 跳过 ==========

c# entity-framework asp.net-core asp.net-core-mvc razor-pages
2个回答
0
投票

在您的加入代码中,您有

on e.EmployeeId equals d.CompanyId
。这不应该是像
on e.CompanyId equals d.CompanyId
这样的东西,以便您匹配在特定公司工作的员工吗?


0
投票

我已经在我的浏览器中查看了 inspect 元素,但没有来自 我的 foreach 循环正在显示

我已尝试重现您的场景,但我正在按预期获取数据。此外,如果您没有在表中设置 primary keyPOCO 模型,您可能会得到空列表。

我尝试了以下方式并获得了预期的结果:

数据库表设计:

公司表:

员工表:

POCO型号:

public class Employee
    {
        [Key]
        public int EmployeeId { get; set; }
        public string First_Name { get; set; }
        public string Last_Name { get; set; }
        public string Company { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public int CompanyId { get; set; }
    }


public class Company
    {
        [Key]
        public int CompanyId { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Logo { get; set; }
        public string Website { get; set; }
       
    }

数据库上下文类:

public class ApplicationDbContext : DbContext
    {
     

        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
           
        }
        public DbSet<Company> Companies { get; set; }
        public DbSet<Employee> Employees { get; set; }
       


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           
            modelBuilder.Entity<Company>().ToTable("CompanyTable");
            modelBuilder.Entity<Employee>().ToTable("EmployeeTable");

        }
    }

注意: 请检查您是否相应地配置了数据库上下文。你可以在这里查看我们的官方文档.

控制器:

public class EmployeeCompanyController : Controller
    {
        private readonly ApplicationDbContext _context;

        public EmployeeCompanyController( ApplicationDbContext context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            List<EmployeeModel> employees = _context.Employees.ToList();
            List<CompanyModel> companies = _context.Companies.ToList();

            var employeeRecord = from e in employees
                                 join d in companies on e.CompanyId equals d.CompanyId
                                 select new ViewModel
                                 {
                                     employee =e,
                                     companies = d,
                                 };
            return View(employeeRecord);

        }
    }

注意: 我使用了您的控制器并以完全相同的方式查看代码。我没有做任何改变。

输出:

© www.soinside.com 2019 - 2024. All rights reserved.