如何在我的网站中加入数据库表?
How to join database tables in my website?
这是我的代码:
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
也许试试:
foreach (var track in Model.Tracks)
{
if(item.AlbumID == Model.AlbumID)
{
<tr>
<td>@track.TrackId</td>
<td>@track.Name</td>
</tr>
}
}
你能post你的模特吗?
我的应用程序中有类似的东西,我创建了 2 个模型,例如1 个用于曲目,1 个用于专辑。
然后在您的专辑模型中制作一个列表 Tracklist,并在您的曲目模型中制作一个 属性 AlbumId。
在你的 trackcontroller 中,在创建轨道时你可以有类似
的东西
var albums = _context.Albums.toList // or any other way to retrieve the albums from the DB.
foreach (var item in albums)
if (track.AlbumId = item.Id)
{
item.Tracklist.add(Track)
}
我对这个还是有点陌生,所以希望它有用!
At the moment I'm displaying all tracks from every album in AlbumTracks.cshtml, instead of just the one selected by the user on album.cshtml.
你确定你描述的是正确的吗?您可以使用以下代码显示所有专辑中的所有曲目:
Tracks = db.Tracks.Include(a => a.Album);
我想你想要的是点击albums.cshtml
中的Tracks
link可以显示所选专辑的曲目:
<td><a asp-page="/Albums/Album Tracks" asp-route-id="@album.AlbumId">Tracks</a></td>
如果我说的是正确的,您需要先将 id
参数添加到您的 /Albums/AlbumTracks
方法并修改 linq,如下所示:
public class AlbumTracksModel : PageModel
{
private Chinook db;
public AlbumTracksModel(Chinook injectedContext)
{
db = injectedContext;
}
public IEnumerable<Track> Tracks { get; set; }
public void OnGetAsync(int id)
{
ViewData["Title"] = "Chinook Web Site - Tracks";
Tracks = db.Tracks.Include(a => a.Album).Where(a=>a.AlbumId==id);
}
}
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>
结果:
这是我的代码:
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
也许试试:
foreach (var track in Model.Tracks)
{
if(item.AlbumID == Model.AlbumID)
{
<tr>
<td>@track.TrackId</td>
<td>@track.Name</td>
</tr>
}
}
你能post你的模特吗?
我的应用程序中有类似的东西,我创建了 2 个模型,例如1 个用于曲目,1 个用于专辑。 然后在您的专辑模型中制作一个列表 Tracklist,并在您的曲目模型中制作一个 属性 AlbumId。 在你的 trackcontroller 中,在创建轨道时你可以有类似
的东西var albums = _context.Albums.toList // or any other way to retrieve the albums from the DB.
foreach (var item in albums)
if (track.AlbumId = item.Id)
{
item.Tracklist.add(Track)
}
我对这个还是有点陌生,所以希望它有用!
At the moment I'm displaying all tracks from every album in AlbumTracks.cshtml, instead of just the one selected by the user on album.cshtml.
你确定你描述的是正确的吗?您可以使用以下代码显示所有专辑中的所有曲目:
Tracks = db.Tracks.Include(a => a.Album);
我想你想要的是点击albums.cshtml
中的Tracks
link可以显示所选专辑的曲目:
<td><a asp-page="/Albums/Album Tracks" asp-route-id="@album.AlbumId">Tracks</a></td>
如果我说的是正确的,您需要先将 id
参数添加到您的 /Albums/AlbumTracks
方法并修改 linq,如下所示:
public class AlbumTracksModel : PageModel
{
private Chinook db;
public AlbumTracksModel(Chinook injectedContext)
{
db = injectedContext;
}
public IEnumerable<Track> Tracks { get; set; }
public void OnGetAsync(int id)
{
ViewData["Title"] = "Chinook Web Site - Tracks";
Tracks = db.Tracks.Include(a => a.Album).Where(a=>a.AlbumId==id);
}
}
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>
结果: