我正在尝试在一个视图页面中显示列表,以获取来自2个不同表/模型的数据。
(Table1)Table_Service:
(Table2)Table_Request:
表1和表2通过ID / Service_Id链接。
我的视图页面应该是(Table2)Table_Request列表:
我的查看页面如下:
@model IEnumerable<OnlinePlatform.Models.ServiceRequests>
<section class="section">
<div class="row">
<div class="col-md-12">
<h2>Please choose a service to start</h2>
<br />
<div class="row">
<div class="container">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Start Date</th>
<th>System Owner</th>
<th>Created By</th>
<th></th>
</tr>
</thead>
@foreach (var item in Model)
{
<tbody>
<tr>
<td>@item.Id</td>
<td>@item.Id</td>
<td>@item.ServiceId</td>
<td>@item.Name</td>
<td>@item.RequestorId</td>
<td>@item.Description</td>
<td>@item.Status</td>
<td><button type="button" class="btn">Edit</button></td>
</tr>
</tbody>
}
</table>
</div>
</div>
</div>
</div>
我的控制器如下:
public ActionResult Service_Request()
{
var dao = new ServicesDAO();
return PartialView("Service_Request", dao.ServiceRequestGetAll().ToList());
}
public IQueryable<ServiceRequests> ServiceRequestGetAll()
{
var result = DB.ServiceRequests.OrderBy(r => r.Id);
return result;
}
如何显示取自(Table1)Table_Service的名称?
答案 0 :(得分:0)
通过使用ID连接到两个表中来获取记录。
以下仅供参考,
var result= from req in DB.Table_Request
join service in DB.Table_Service
on req.Service_id equals service.id
select new ServiceRequests
{
Name=service.Name,
ServiceID=req.Service_Id,
// Followed model Properties
}.ToList();
答案 1 :(得分:0)
我建议您创建这样的服务:
IServiceManager.cs :
public interface IServiceManager
{
IEnumerable<ServiceRequests> GetAllServiceRequests();
Service GetService(string serviceId);
}
ServiceManager.cs :
public class ServiceManager : IServiceManager
{
public ApplicationDbContext _dbContext { get; set; }
public ServiceRequest(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public IEnumerable<ServiceRequests> GetAllServiceRequests()
=> _dbContext.ServiceRequests.OrderBy(r => r.Id);
public Service GetService(string serviceId)
=> !string.IsNullOrEmpty(serviceId)
? _dbContext.Services.SingleOrDefault(x => x.Id == serviceId) : null;
}
Startup.cs :
services.AddTransient<IServiceManager, ServiceManager>();
控制器:
public ActionResult Service_Request()
{
return View();
}
Service_Request.cshtml :
@model IEnumerable<OnlinePlatform.Models.ServiceRequests>
@inject IServiceManager serviceManager
@foreach (var request in serviceManager.GetAllServiceRequests())
{
var service = serviceManager.GetService(request.Service_Id);
<!-- you can use @service.Name here... -->
}
答案 2 :(得分:0)
由于您希望将混合结果集返回为IQueryable<T>
,因此应使用Join()
查询并将查询结果放入视图模型:
1)Lambda版本
public IEnumerable<ServiceRequestsVM> ServiceRequestGetAll()
{
var result = DB.ServiceRequests.Join(DB.Services,
x => x.Service_Id, // Source table foreign key
y => y.Id, // Joined table primary key
(x, y) => new ServiceRequestsVM { Id = x.Id, Service_Id = x.Service_Id, Name = y.Name, Description = x.Description, Requestor = x.Requestor, Status = x.Status })
.OrderBy(z => z.Service_Id).ToList();
return result;
}
2)查询表达式版本
public IEnumerable<ServiceRequestsVM> ServiceRequestGetAll()
{
var result = (from srv in DB.Services
join srq in DB.ServiceRequests
on srv.Id equals srq.Service_Id
select new ServiceRequestsVM
{
Id = srq.Id,
Service_Id = srq.Service_Id,
Name = srv.Name,
Description = srq.Description,
Requestor = srq.Requestor,
Status = srq.Status
}).OrderBy(z => z.Service_Id).ToList();
return result;
}
视图模型示例
public class ServiceRequestsVM
{
public int Id { get; set; }
public int Service_Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Requestor { get; set; }
public string Status { get; set; }
}
然后将@model
指令更改为将viewmodel与两个表的组合属性一起使用:
@model IEnumerable<OnlinePlatform.Models.ServiceRequestsVM>
控制器应该看起来像这样:
public ActionResult Service_Request()
{
var dao = new ServicesDAO();
return PartialView("Service_Request", dao.ServiceRequestGetAll());
}
完成这些步骤后,模型绑定应完全起作用。