MVC ASP.NET不使用连接显示来自数据库的任何结果

huangapple go评论56阅读模式
英文:

MVC ASP.NET not showing any results from database using join

问题

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

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>

我已经调试了应用程序,但没有发现任何问题;当我构建应用程序时,它显示构建成功 ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========。

英文:

I have created an MVC application that is supposed to join two tables from a Database using LINQ; when the application is run there are no results showing. Additionally, I have viewed the inspect element in my browser, and no tables from my foreach loop are showing?

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&lt;Employee&gt; employees = db.Employees.ToList();
                   List&lt;Company&gt; 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&lt;WorkingWithMultipleDataTables.Models.ViewModel&gt;
   @{
       Layout = null;
    }

        &lt;table class=&quot;table table-striped table-bordered&quot;&gt;
            &lt;thead class=&quot;bg-dark text-white&quot;&gt;
                &lt;tr&gt;
                    &lt;th&gt;EmployeeId&lt;/th&gt;
                    &lt;th&gt;First_Name&lt;/th&gt;
                    &lt;th&gt;Last_Name&lt;/th&gt;
                    &lt;th&gt;Company&lt;/th&gt;
                    &lt;th&gt;Email&lt;/th&gt;
                    &lt;th&gt;Phone&lt;/th&gt;

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

                        &lt;td&gt;@item.companies.CompanyId&lt;/td&gt;
                        &lt;td&gt;@item.companies.Name&lt;/td&gt;
                        &lt;td&gt;@item.companies.Email&lt;/td&gt;
                        &lt;td&gt;@item.companies.Logo&lt;/td&gt;
                        &lt;td&gt;@item.companies.Website&lt;/td&gt;


                    &lt;/tr&gt;

                }
            &lt;/tbody&gt;
        &lt;/table&gt;

I have debugged the application but no issues were found; when I build the application it shows a successful build ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

答案1

得分: 0

在你的连接代码中,你有 on e.EmployeeId equals d.CompanyId。这应该是类似 on e.CompanyId equals d.CompanyId,以便匹配在特定公司工作的员工,不是吗?

英文:

In your join code you have on e.EmployeeId equals d.CompanyId. Shouldn't that be something like on e.CompanyId equals d.CompanyId, so that you match employees that work in a specific company?

答案2

得分: 0

我已查看了浏览器中的检查元素,但我的foreach循环未显示任何表格。

我已尝试复制您的情景,但我按预期获取了数据。此外,如果您在表格和POCO模型中没有设置主键,则可能会得到空列表。

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

数据库表设计:

公司表:
MVC ASP.NET不使用连接显示来自数据库的任何结果

员工表:
MVC ASP.NET不使用连接显示来自数据库的任何结果

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);
    }
}

注意: 我已完全按照您的控制器和视图代码使用,没有做任何更改。

输出:

MVC ASP.NET不使用连接显示来自数据库的任何结果

英文:

> I have viewed the inspect element in my browser, and no tables from
> my foreach loop are showin

I have tried to reproduce your scenario but I am getting the data as expected. Furthermore, you might ended up with empty list if you haven't set primary key within your table and POCO model.

I have tried in following way and getting expected result:

Database Table Design:

Company Table:

MVC ASP.NET不使用连接显示来自数据库的任何结果

Employee Table:

MVC ASP.NET不使用连接显示来自数据库的任何结果

POCO Model:

<!-- language: c# -->
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; }
       
    }

Db Context Class:

<!-- language: c# -->
public class ApplicationDbContext : DbContext
{

        public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options) : base(options)
        {
           
        }
        public DbSet&lt;Company&gt; Companies { get; set; }
        public DbSet&lt;Employee&gt; Employees { get; set; }
       


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
           
            modelBuilder.Entity&lt;Company&gt;().ToTable(&quot;CompanyTable&quot;);
            modelBuilder.Entity&lt;Employee&gt;().ToTable(&quot;EmployeeTable&quot;);

        }
    }

Note: Please check if you configured your database context accordingly. You can have a look our official document here.

Controller:

<!-- language: c# -->
public class EmployeeCompanyController : Controller
{
private readonly ApplicationDbContext _context;

        public EmployeeCompanyController( ApplicationDbContext context)
        {
            _context = context;
        }
        public IActionResult Index()
        {
            List&lt;EmployeeModel&gt; employees = _context.Employees.ToList();
            List&lt;CompanyModel&gt; 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);

        }
    }

Note: I have used your controller and view code exactly the same way. No change I made.

Output:

MVC ASP.NET不使用连接显示来自数据库的任何结果

huangapple
  • 本文由 发表于 2023年3月3日 19:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定