如何在asp.net mvc-core中读取和显示相关数据?

时间:2018-12-12 06:31:45

标签: asp.net-core-mvc

作为我可以建立的样本,我想使用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>

2 个答案:

答案 0 :(得分:0)

由于navigation propertyClub之间没有Teams,因此您需要基于ClubName查询ClubNo。并且由于ClubName中没有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);
}

答案 1 :(得分:0)

这两个文档非常有帮助。

https://docs.microsoft.com/en-us/ef/core/modeling/relationships

https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/read-related-data?view=aspnetcore-2.2&tabs=visual-studio

这旨在作为建立相关数据其他要求的示例。

我的Club.cs,Ground.cs和Teams.cs模型的一部分

public partial class Club
{
    public Club()
    public short ClubNo { get; set; }
    public string ClubName { get; set; }
    public string PhoneNo { get; set; }
}

public partial class Ground
{
    public Ground()
    public short GroundNo { get; set; }
    public string GroundName { get; set; }
    public string Address { 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的一部分,以显示“索引”页面。 公共类TeamsController:控制器 {     私有只读SSFA_SQLContext _context;

public TeamsController(SSFA_SQLContext context)
{
    _context = context;
}

// GET: Teams
public async Task<IActionResult> Index(string filter, 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);
}

对象是在“团队索引”页面上显示“俱乐部名称”和“地面名称”

解决方案:(执行此操作的许多可能方法中的大多数)。

在Teams context.cs文件中添加以下内容:(无需更改Club或Ground上下文文件)

 entity.HasOne(q => q.ClubNavigation);
 entity.HasOne(q => q.GroundNavigation);

在Teams.cs模型文件中添加以下内容:(无需更改Club或Ground模型文件)

public Club ClubNavigation { get; set; }
public Ground GroundNavigation { get; set; }

在上方添加[ForeignKey(“ GroundNavigation”)]

public short HomeGround { get; set; }

(这是必需的,因为HomeGround必须找到GroundNo)

在上方添加[ForeignKey(“ ClubNavigation”)]

public short ClubNo { get; set; }

由于两个文件中的ClubNo都相同,因此第二个外键不是必需的,但我认为保持一致性很高兴。

在TeamsController.cs中更改

var qry = _context.Teams
            .AsNoTracking()
            .AsQueryable();

收件人

var qry = _context.Teams
            .Include(q => q.ClubNavigation)
            .Include(q => q.GroundNavigation)
            .AsNoTracking()
            .AsQueryable();

在“团队索引”文件中添加以下内容:

<th>@Html.DisplayNameFor(model => model.HomeGround)</th>
<td><b>@Html.DisplayNameFor(model => model.GroundNavigation.GroundName)</b></td>
<th>@Html.DisplayNameFor(model => model.ClubNo)</th>
<td><b>@Html.DisplayNameFor(model => model.ClubNavigation.ClubName)</b></td>

<td>@Html.DisplayFor(modelItem => item.HomeGround)</td>
<td>@Html.DisplayFor(modelItem => item.GroundNavigation.GroundName)</td>
<td>@Html.DisplayFor(modelItem => item.ClubNo)</td>
<td>@Html.DisplayFor(modelItem => item.ClubNavigation.ClubName)</td>

就是这样。