英文:
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<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>
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模型中没有设置主键,则可能会得到空列表。
我尝试了以下方法并获得了预期的结果:
数据库表设计:
公司表:
员工表:
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);
}
}
注意: 我已完全按照您的控制器和视图代码使用,没有做任何更改。
输出:
英文:
> 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:
Employee Table:
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<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");
}
}
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<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);
}
}
Note: I have used your controller and view code exactly the same way. No change I made.
Output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论