首页 > 解决方案 > 如何在 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>

标签: asp.net-core-mvc

解决方案


由于没有navigation propertybetween Cluband Teams,所以需要查询ClubNamebased on ClubNo。并且由于没有ClubNamein 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);
}

推荐阅读