【发布时间】:2021-08-12 20:33:46
【问题描述】:
这是我的代码:
albums.cshtml
@page
@using Project.Models
@model Project.Pages.AlbumsModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<div class="row">
<h1 class="display-2">Albums</h1>
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Album ID</th>
<th>Artist ID</th>
<th>Artist name</th>
<th>Album title</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (Album album in Model.Albums)
{
<tr>
<td>@album.AlbumId</td>
<td>@album.ArtistId</td>
<td>@Html.DisplayFor(modelArtists => album.Artist.Name)</td>
<td>@album.Title</td>
<td><a asp-page="/Albums/Details" asp-route-id="@album.AlbumId">Details</a></td>
<td><a asp-page="/Albums/Edit" asp-route-id="@album.AlbumId">Edit</a></td>
<td><a asp-page="/Albums/Delete" asp-route-id="@album.AlbumId">Delete</a></td>
<td><a asp-page="/Albums/AlbumTracks" asp-route-id="@album.AlbumId">Tracks</a></td>
</tr>
}
</tbody>
</table>
</div>
<div class="row">
<p>Enter a title & ArtistID for a new album: </p>
<form method="POST">
<div><input asp-for="Album.Title" /></div>
<div><input asp-for="Album.ArtistId" /></div>
<input type="submit" />
</form>
</div>
<div>
<a asp-page="/Index">Home</a>
</div>
AlbumTracks.cshtml
@page
@using Project.Models
@model Project.Pages.AlbumTracksModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<div class="row">
<h1 class="display-2">Tracks</h1>
<table class="table">
<thead class="thead-inverse">
<tr>
<th>Track name</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (Track track in Model.Tracks)
{
<tr>
<td>@track.TrackId</td>
<td>@track.Name</td>
</tr>
}
</tbody>
</table>
</div>
@* <div class="row">
<p>Enter a name & TrackID for a new track: </p>
<form method="POST">
<div><input asp-for="Track.Name" /></div>
<div><input asp-for="Track.TrackId" /></div>
<input type="submit" />
</form>
</div> *@
<div>
<a asp-page="/Index">Home</a>
</div>
AlbumTracks.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Project.Models;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Project.Pages
{
public class AlbumTracksModel : PageModel
{
private Chinook db;
public AlbumTracksModel(Chinook injectedContext)
{
db = injectedContext;
}
public IEnumerable<Track> Tracks { get; set; }
public void OnGetAsync()
{
ViewData["Title"] = "Chinook Web Site - Tracks";
Tracks = db.Tracks.Include(a => a.Album);
}
}
}
目前,我在 AlbumTracks.cshtml 中显示每个专辑中的所有曲目,而不仅仅是用户在 album.cshtml 中选择的曲目。我已经反复阅读this,但我正在努力找出 AlbumTracks.cshtml 的语法,也许我需要在 AlbumTracks.cshtml.cs 中添加更多内容?
我想也许我需要使用这样的东西来加入表格:
if (item.AlbumID == Model.AlbumID)
请指点一下? TIA。
编辑:GitHub repo
【问题讨论】:
标签: c# asp.net entity-framework asp.net-core razor-pages