[英]Razor Pages - EF return values of 2 tables
I have a database with 2 tables (clients and cars).我有一个包含 2 个表(客户和汽车)的数据库。 The table cars have a column named client_id.
表cars 有一列名为client_id。 In my Razor Pages project I have the models created for the 2 tables, but I need do a INNER JOIN to return the result to view side and do a foreach loop.
在我的 Razor Pages 项目中,我为 2 个表创建了模型,但我需要执行 INNER JOIN 以将结果返回到视图端并执行 foreach 循环。 So until now I used the IList and do this in .cshtml:
所以直到现在我使用 IList 并在 .cshtml 中执行此操作:
@foreach (var item in Model.clients)
{
<p>@Html.DisplayFor(x=> item.name)</p>
<p>@Html.DisplayFor(x=> item.mail)</p>
}
And in code cshtm.cs并在代码 cshtm.cs
public IList<establishments> establishments;
IQueryable<establishments> establishments_filter;
establishments_filter = (from x in db.establishments where x.category == category select x);
establishments = establishments_filter.ToList();
Now the problem is that I cant do the same with a Inner Join or I don´t know how (most probably).现在的问题是我不能用内部连接做同样的事情,或者我不知道如何(最有可能)。 I see in others posts that I can use a variable to receive the values like this:
我在其他帖子中看到我可以使用变量来接收这样的值:
var filter = (from x in db.cars join y in db.clients on x.id_client == y.id select new {
mark = x.mark,
model = x.model,
name = y.name,
mail = y.name
}.ToList();
But now, my real question... How I can do a foreach if the var filter is not acessible in cshtml?但是现在,我真正的问题是...如果在 cshtml 中无法访问 var 过滤器,我该如何进行 foreach?
Thanks谢谢
I think you can define a viewModel that contains the field in the filter
object.我认为您可以定义一个包含
filter
对象中的字段的 viewModel。 Then you can foreach the viewModel in your page.然后你可以在你的页面中 foreach 视图模型。 A simple demo like below:
一个简单的演示如下:
Model:模型:
public class Car
{
public int id { get; set; }
public string mark { get; set; }
public string model { get; set; }
[ForeignKey("Client")]
public int client_id { get; set; }
}
public class Client
{
public int id { get; set; }
public string name { get; set; }
public string mail { get; set; }
}
ViewModel:视图模型:
public class ViewModel
{
public string Mark { get; set; }
public string Model { get; set; }
public string Name { get; set; }
public string Mail { get; set; }
}
Index.cshtml:索引.cshtml:
@page
@model RazorPageApp.Pages.IndexModel
@{
ViewData["Title"] = "Index";
}
<table>
<thead>
<tr>
<th>Mark</th>
<th>Model</th>
<th>Name</th>
<th>Mail</th>
</tr>
</thead>
@foreach (var item in Model.filter)
{
<tr>
<td>@Html.DisplayFor(x => item.Mark)</td>
<td>@Html.DisplayFor(x => item.Model)</td>
<td>@Html.DisplayFor(x => item.Name)</td>
<td>@Html.DisplayFor(x => item.Mail)</td>
</tr>
}
</table>
Index.cshtml.cs:索引.cshtml.cs:
public class IndexModel : PageModel
{
private readonly ILogger<IndexModel> _logger;
private readonly MyDbContext _db;
public IndexModel(ILogger<IndexModel> logger, MyDbContext db)
{
_db = db;
_logger = logger;
}
public List<ViewModel> filter { get; set; }
public async Task OnGetAsync()
{
filter = await (from x in _db.cars
join y in _db.clients on x.client_id equals y.id
select new ViewModel
{
Mark = x.mark,
Model = x.model,
Name = y.name,
Mail = y.mail
}).ToListAsync();
}
DataSource:数据源:
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.