asp.net mvc 实现博客文章的用户回复功能
时间: 2023-08-01 22:15:13 浏览: 79
实现博客文章的用户回复功能需要以下步骤:
1. 创建评论模型
创建一个评论模型,包含评论的内容、评论时间、评论者等信息。例如:
```
public class Comment
{
public int Id { get; set; }
[Required]
public string Content { get; set; }
public DateTime CreatedAt { get; set; }
public string UserId { get; set; }
public virtual ApplicationUser User { get; set; }
public int PostId { get; set; }
public virtual Post Post { get; set; }
}
```
2. 创建评论控制器
创建一个评论控制器,包含添加评论和删除评论的方法。例如:
```
public class CommentController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
[HttpPost]
public ActionResult Create(int postId, string content)
{
var comment = new Comment
{
Content = content,
CreatedAt = DateTime.Now,
UserId = User.Identity.GetUserId(),
PostId = postId
};
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Details", "Post", new { id = postId });
}
[HttpPost]
public ActionResult Delete(int id)
{
var comment = db.Comments.Find(id);
if (comment != null && comment.UserId == User.Identity.GetUserId())
{
db.Comments.Remove(comment);
db.SaveChanges();
}
return RedirectToAction("Index", "Home");
}
}
```
3. 添加评论视图
在文章详情页中添加评论视图,包含评论表单和已有评论列表。例如:
```
@model PostViewModel
...
<h2>Comments</h2>
@if (User.Identity.IsAuthenticated)
{
<form action="@Url.Action("Create", "Comment")" method="post">
@Html.AntiForgeryToken()
<div class="form-group">
<label for="content">Content:</label>
<textarea name="content" class="form-control"></textarea>
</div>
<input type="hidden" name="postId" value="@Model.Post.Id" />
<button type="submit" class="btn btn-primary">Submit</button>
</form>
}
else
{
<p>Please <a href="@Url.Action("Login", "Account")">log in</a> to leave a comment.</p>
}
@if (Model.Comments.Any())
{
<ul class="list-group">
@foreach (var comment in Model.Comments)
{
<li class="list-group-item">
<div>
<strong>@comment.User.UserName</strong>
<span class="text-muted">@comment.CreatedAt.ToString("yyyy-MM-dd HH:mm:ss")</span>
</div>
<div>@comment.Content</div>
@if (User.Identity.IsAuthenticated && comment.UserId == User.Identity.GetUserId())
{
<form action="@Url.Action("Delete", "Comment")" method="post">
@Html.AntiForgeryToken()
<input type="hidden" name="id" value="@comment.Id" />
<button type="submit" class="btn btn-link">Delete</button>
</form>
}
</li>
}
</ul>
}
else
{
<p>No comments yet.</p>
}
```
4. 更新文章详情页
在文章详情页中添加评论视图,显示已有评论并允许用户添加评论。例如:
```
public class PostController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult Details(int id)
{
var post = db.Posts.Find(id);
if (post == null)
{
return HttpNotFound();
}
var comments = db.Comments
.Where(c => c.PostId == id)
.Include(c => c.User)
.OrderByDescending(c => c.CreatedAt)
.ToList();
var model = new PostViewModel
{
Post = post,
Comments = comments
};
return View(model);
}
}
```
以上就是ASP.NET MVC实现博客文章的用户回复功能的步骤。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)