asp.net-core-mvc - 如何在 asp.net mvc-core 中读取和显示相关数据?
问题描述
作为我可以构建的示例,我想使用它Teams.ClubNo
来查找Club.ClubNo
并添加Club.ClubName
到团队索引页面。
听起来很简单,但我还没有找到任何可行的方法。
过滤器(我喜欢)混淆了我需要在控制器中执行的操作。数据库中不存在直接导航。我正在使用 EF 2.1
_context.Club 和 _context.Teams 的一部分
public partial class Club
{
public Club()
public short ClubNo { get; set; }
public string ClubName { get; set; }
}
public partial class Teams
{
public Teams()
public string Division { get; set; }
public string Grade { get; set; }
public short HomeGround { get; set; }
public short ClubNo { get; set; }
}
我的 TeamsController.cs 的一部分,用于显示我的索引页面。
public class TeamsController : Controller
{
private readonly SSFA_SQLContext _context;
public TeamsController(SSFA_SQLContext context)
{
_context = context;
}
// GET: Teams
public async Task<IActionResult> Index(string filter, string filter1, string filter2, int page = 1, string sortExpression = "Division")
{
var qry = _context.Teams.AsNoTracking().AsQueryable();
if (!string.IsNullOrWhiteSpace(filter))
{ qry = qry.Where(p => p.Division.Contains(filter)); }
var model = await PagingList.CreateAsync(qry, 15, page, sortExpression, "Division");
model.RouteValue = new RouteValueDictionary { "filter", filter } ;
return View(model);
}
我的团队索引页面的一部分。
<table class="table">
<thead>
<tr>
<td>
@Html.DisplayNameFor(model => model.Division)
</td>
<td>
@Html.DisplayNameFor(model => model.Grade)
</td>
<th>
@Html.DisplayNameFor(model => model.ClubNo)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Division)
</td>
<td>
@Html.DisplayFor(modelItem => item.Grade)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClubNo)
</td>
解决方案
由于没有navigation property
between Club
and Teams
,所以需要查询ClubName
based on ClubNo
。并且由于没有ClubName
in Teams
,您需要定义一个新模型,其中包含Clubname
.
TeamsVM.cs
public partial class TeamsVM
{
public string Division { get; set; }
public string Grade { get; set; }
public string ClubName { get; set; }
}
并更改如下查询:
public async Task<IActionResult> Index1(string filter, string filter1, string filter2, int page = 1, string sortExpression = "Division")
{
var qry = _applicationDbContext.Teams.AsNoTracking().AsQueryable();
if (!string.IsNullOrWhiteSpace(filter))
{ qry = qry.Where(p => p.Division.Contains(filter)); }
var result = qry.Select(q => new TeamsVM
{
Division = q.Division,
Grade = q.Grade,
ClubName = _applicationDbContext.Club.FirstOrDefault(c => c.ClubNo == q.ClubNo).ClubName
});
var model = await PagingList.CreateAsync(result, 15, page, sortExpression, "Division");
model.RouteValue = new RouteValueDictionary { "filter", filter };
return Ok(result);
}
推荐阅读
- python - 在 for 中找到 Python .append 时不起作用,否则
- linux - 如何使程序在 SHELL 中的任何位置都可执行
- sympy - 如何使用 SymPy 内置模块从函数中得出比率?
- bash - 如何存储 awk 命令的输出,该命令是使用 shell 脚本程序从管道中获取输入到数组中的
- javascript - Net Core:TextBox 中的屏蔽值和密码
- swift - Swift 类初始化器中的可选类初始化器参数
- laravel - Laravel Carbon 中的两个时差显示错误
- ruby-on-rails - 开发中的错误:资产未声明为在生产中预编译
- laravel - 有没有办法(管理)在 laravel 中添加毫秒(到 now() )?
- azure-iot-hub - 从 Azure IoT Hub 升级固件