论坛管理
EF Core Include
EF Core 通过 Include 实现 JOIN SQL 查询。
例如在展示论坛信息时需同时展示论坛拥有者的信息,所以 Include 了 Owner:
[HttpGet]
public IActionResult Index()
{
var forums = _dbContext.Forum.Include("Owner").ToList();
return View(forums);
}
Paritial View
因为论坛详情将在两个地方用到,所以使用了 Partial View
展示论坛详情
删除论坛时展示一下,帮助用户判断到底删不删
在 Views\Shared 文件夹下创建分部视图 _ForumDetail。
@using CoreBB.Web.Models
@model Forum
<div class="container col-6 offset-3" style="margin-top:120px">
<div class="card border-info">
<div class="card-header text-white bg-info">
<strong>Forum Detail Information</strong>
</div>
<div class="card-body">
<table class="table table-bordered table-striped">
<tr>
<th>Property</th>
<th>Value</th>
</tr>
<tr>
<td><strong>ID</strong></td>
<td>@Model.Id</td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>@Model.Name</td>
</tr>
<tr>
<td><strong>Owner</strong></td>
<td><a asp-action="Detail" asp-controller="User" asp-route-name="@Model.Owner.Name">@Model.Owner.Name</a></td>
</tr>
<tr>
<td><strong>Description</strong></td>
<td>@Model.Description</td>
</tr>
<tr>
<td><strong>Locked</strong></td>
<td><input type="checkbox" asp-for="IsLocked" disabled="disabled" /></td>
</tr>
<tr>
<td><strong>Create Date</strong></td>
<td>@Model.CreateDateTime.ToShortDateString()</td>
</tr>
<tr>
<td><strong>Operation</strong></td>
<td>
@if ((string)ViewData["Mode"] == "ShowingDetail")
{
if(User.IsInRole(Roles.Administrator))
{
<a class="btn-sm btn-primary" asp-action="Edit" asp-route-id="@Model.Id">Update</a>
<a class="btn-sm btn-warning" asp-action="Delete" asp-route-id="@Model.Id">Delete</a>
}
<a class="btn-sm btn-success" asp-controller="Topic" asp-action="Create" asp-route-forumid="@Model.Id">Post Topic</a>
}
@if ((string)ViewData["Mode"] == "ConfirmingDelete")
{
<input type="submit" class="btn-sm btn-warning" value="Confirm" />
}
</td>
</tr>
</table>
</div>
</div>
</div>
关于该分部视图的几个要点:
分部视图有自己的 View Model
在 Parent View(父视图)中通过 Html.Partial 设置传递给父视图的 View Model
通过设置 ViewData[“Mode”] 的值显示/隐藏 UI 元素
使用分部视图
在 Detail 视图里面使用分部视图:
@using CoreBB.Web.Models
@model Forum
@{
ViewBag.Title = "Forum Detail";
ViewData["Mode"] = "ShowingDetail";
}
@Html.Partial("_ForumDetail", Model)
使用 @Html.Partial(“_ForumDetail”, Model) 渲染 Partial View,并传递 View Model
Partial View 共享 Parent 的 ViewBag 和 ViewData
在 Delete 视图里面使用分部视图:
@model CoreBB.Web.Models.Forum
@{
ViewBag.Title = "Delete Forum";
ViewData["Mode"] = "ConfirmingDelete";
}
<h2 class="text-warning">Warning: All topics in this forum will be also deleted!</h2>
<form asp-action="Delete" method="post">
<input type="hidden" asp-for="Id" />
@Html.Partial("_ForumDetail", Model)
</form>
model vs Model
@model 语句用于设置视图的 View Model
Model 是视图 View Model 的具体引用
它俩的关系好比一个设置类型,一个引用实例。
帖子管理
与论坛不同,帖子间是有关系的,直接创建(Create)的是根帖子(root topic),通过回复(Reply)创建的是子帖子。 这些帖子一起构成了树形结构。
消息管理
消息显式时需依据发出/接收和已读/未读分为好几个类别,对于这类数据的传递有多种方式:
直接传递整个 Message 集合,在 View 里面再具体筛选
传递 List
- >
使用 Dictionary
>
课程中使用的方式是传递整个集合,在 View 里面筛选:
@model IEnumerable<CoreBB.Web.Models.Message>
@inject CoreBB.Web.Models.CoreBBContext _dbContext
@{
ViewBag.Title = "Messages";
var user = _dbContext.User.Single(u => u.Name == User.Identity.Name);
var unread = Model.Where(m => m.ToUserId == user.Id && !m.IsRead);
var read = Model.Where(m => m.ToUserId == user.Id && m.IsRead);
var unreadSent = Model.Where(m => m.FromUserId == user.Id && !m.IsRead);
var readSent = Model.Where(m => m.FromUserId == user.Id && m.IsRead);
}
<div class="container" style="margin-top:60px">
<div class="card border-info">
<div class="card-header text-white bg-info">
<strong>Messages</strong>
<span>[Uread:@unread.Count()]</span>
<span>[Read:@read.Count()]</span>
<span>[Uread Sent:@unreadSent.Count()]</span>
<span>[Read Sent:@readSent.Count()]</span>
</div>
<div class="card-body">
<h3>Unread Messages</h3>
@Html.Partial("_MessageList", unread)
<h3>Read Messages</h3>
@Html.Partial("_MessageList", read)
<h3>Unread Sent Messages</h3>
@Html.Partial("_MessageList", unreadSent)
<h3>Read Sent Messages</h3>
@Html.Partial("_MessageList", readSent)
</div>
</div>
</div>