您能告诉我通过使用ASP.NET MVC中的发布ID添加评论以邮寄
我是ASP.NET MVC的新手,我想知道如何使用POST ID在帖子上添加评论。
在这里,我展示了所有评论,而不是特定的帖子评论。我认为我应该在控制器中添加代码才能做到这一点。
这是这里的评论和邮寄
评论
using System.ComponentModel.DataAnnotations;
namespace Post.Models
{
public class Comments
{
public int Id { get; set; }
[Required(ErrorMessage = "Please Enter Name")]
public string Name { get; set; }
[Required]
public string Comment { get; set; }
[Required]
[RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,6}$", ErrorMessage = "Please provide Valid email")]
public string Email { get; set; }
public string dateTime { get; set; } = DateTime.Now.ToString();
public int PostId { get; set; }
public Post Post { get; set; }
}
}
帖子,
using Post.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace BlogTutorial.ViewModel
{
public class PostVM
{
[Required(ErrorMessage = "Please Enter Title")]
public string Title { get; set; }
[Required(ErrorMessage ="Please provide subtitle")]
[RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,6}$", ErrorMessage = "Please provide Valid email")]
public string SubTitle { get; set; }
public string Content { get; set; }
public string Date { get; set; }
[DisplayName("Cover image for blog post")]
public IFormFile Image { get; set; }
public string Slug { get; set; }
public IEnumerable<Comments> Comments { get; set; }
}
}
这里是控制器代码:
public void SharedLayOutData()
{
ViewBag.Post = db.Tbl_Post;
ViewBag.Comments = db.Tbl_Comments;
ViewBag.Profile = db.Tbl_Profile.FirstOrDefault();
}
public IActionResult AddComment()
{
return View();
}
[HttpPost]
public IActionResult AddComment(Comments mycomment)
{
db.Tbl_Comments.Add(mycomment);
db.SaveChanges();
return RedirectToAction("Post", "Home");
}
添加帖子
public IActionResult AddPost()
{
if (HttpContext.Session.GetString("LoginFlag") != null)
{
DisplayData();
return View();
}
else
{
//return RedirectToAction("Login", "Admin");
return Redirect("/Admin/Login?ReturnUrl=/Admin/AddPost");
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AddPost(PostVM myPost)
{
if (HttpContext.Session.GetString("LoginFlag") != null)
{
DisplayData();
if (ModelState.IsValid)
{
string ImageName = myPost.Image.FileName.ToString();
var FolderPath = Path.Combine(env.WebRootPath, "images");
var CompletePicPath = Path.Combine(FolderPath, ImageName);
myPost.Image.CopyTo(new FileStream(CompletePicPath, FileMode.Create));
Post post = new Post();
post.Title = myPost.Title;
post.SubTitle = myPost.SubTitle;
post.Date = myPost.Date;
post.Slug = myPost.Slug;
post.Content = myPost.Content;
post.Image = ImageName;
db.Tbl_Post.Add(post);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View();
}
return RedirectToAction("Login", "Admin");
}
是视图
@using Post.Models
@using System.Web;
@model Post
@{
IEnumerable<Comments> mycomments = ViewBag.Comments;
}
<!-- Blog entry -->
<div class="w3-card-4 w3-margin w3-white">
<img src="~/images/@Model.Image" alt="Nature" style="width:100%">
<div class="w3-container">
<hr>
<h3><b>@Model.Title</b></h3>
<hr>
<h5>@Model.SubTitle ,<span class="w3-opacity">@Model.Date</span></h5>
<hr>
</div>
<div class="w3-container">
<p>
@Html.Raw(HttpUtility.HtmlDecode(Model.Content))
</p>
<div class="w3-row">
</div>
</div>
</div>
<div class="container comment_section">
<section class="comments_top">
<div class="container">
<div class="row">
<div class="col-sm-5 col-md-6 col-12 pb-4">
<h1>Comments</h1>
@foreach(var item in mycomments){
<div class="comment mt-4 text-justify float-left">
<img src="https://i.imgur.com/yTFUilP.jpg" alt="" class="rounded-circle" width="40" height="40">
<h4>@item.Name</h4>
<span>@item.dateTime</span>
<br>
<p>@item.Comment</p>
</div>
}
</div>
<div class="col-lg-4 col-md-5 col-sm-4 offset-md-1 offset-sm-1 col-12 mt-4">
</div>
</div>
</div>
</section>
</div>
,请告诉我如何检索帖子ID的特定帖子的评论
I am new to ASP.NET MVC and I want to know how to add comment on post by using post id.
Here I am showing all the comment not specific post comment. I think I should have to add code in controller to do this.
Here is the comment and post model
Comment
using System.ComponentModel.DataAnnotations;
namespace Post.Models
{
public class Comments
{
public int Id { get; set; }
[Required(ErrorMessage = "Please Enter Name")]
public string Name { get; set; }
[Required]
public string Comment { get; set; }
[Required]
[RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,6}quot;, ErrorMessage = "Please provide Valid email")]
public string Email { get; set; }
public string dateTime { get; set; } = DateTime.Now.ToString();
public int PostId { get; set; }
public Post Post { get; set; }
}
}
Post
using Post.Models;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace BlogTutorial.ViewModel
{
public class PostVM
{
[Required(ErrorMessage = "Please Enter Title")]
public string Title { get; set; }
[Required(ErrorMessage ="Please provide subtitle")]
[RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,6}quot;, ErrorMessage = "Please provide Valid email")]
public string SubTitle { get; set; }
public string Content { get; set; }
public string Date { get; set; }
[DisplayName("Cover image for blog post")]
public IFormFile Image { get; set; }
public string Slug { get; set; }
public IEnumerable<Comments> Comments { get; set; }
}
}
Here is the controller code:
public void SharedLayOutData()
{
ViewBag.Post = db.Tbl_Post;
ViewBag.Comments = db.Tbl_Comments;
ViewBag.Profile = db.Tbl_Profile.FirstOrDefault();
}
public IActionResult AddComment()
{
return View();
}
[HttpPost]
public IActionResult AddComment(Comments mycomment)
{
db.Tbl_Comments.Add(mycomment);
db.SaveChanges();
return RedirectToAction("Post", "Home");
}
Add Post
public IActionResult AddPost()
{
if (HttpContext.Session.GetString("LoginFlag") != null)
{
DisplayData();
return View();
}
else
{
//return RedirectToAction("Login", "Admin");
return Redirect("/Admin/Login?ReturnUrl=/Admin/AddPost");
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult AddPost(PostVM myPost)
{
if (HttpContext.Session.GetString("LoginFlag") != null)
{
DisplayData();
if (ModelState.IsValid)
{
string ImageName = myPost.Image.FileName.ToString();
var FolderPath = Path.Combine(env.WebRootPath, "images");
var CompletePicPath = Path.Combine(FolderPath, ImageName);
myPost.Image.CopyTo(new FileStream(CompletePicPath, FileMode.Create));
Post post = new Post();
post.Title = myPost.Title;
post.SubTitle = myPost.SubTitle;
post.Date = myPost.Date;
post.Slug = myPost.Slug;
post.Content = myPost.Content;
post.Image = ImageName;
db.Tbl_Post.Add(post);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View();
}
return RedirectToAction("Login", "Admin");
}
Here is the view
@using Post.Models
@using System.Web;
@model Post
@{
IEnumerable<Comments> mycomments = ViewBag.Comments;
}
<!-- Blog entry -->
<div class="w3-card-4 w3-margin w3-white">
<img src="~/images/@Model.Image" alt="Nature" style="width:100%">
<div class="w3-container">
<hr>
<h3><b>@Model.Title</b></h3>
<hr>
<h5>@Model.SubTitle ,<span class="w3-opacity">@Model.Date</span></h5>
<hr>
</div>
<div class="w3-container">
<p>
@Html.Raw(HttpUtility.HtmlDecode(Model.Content))
</p>
<div class="w3-row">
</div>
</div>
</div>
<div class="container comment_section">
<section class="comments_top">
<div class="container">
<div class="row">
<div class="col-sm-5 col-md-6 col-12 pb-4">
<h1>Comments</h1>
@foreach(var item in mycomments){
<div class="comment mt-4 text-justify float-left">
<img src="https://i.imgur.com/yTFUilP.jpg" alt="" class="rounded-circle" width="40" height="40">
<h4>@item.Name</h4>
<span>@item.dateTime</span>
<br>
<p>@item.Comment</p>
</div>
}
</div>
<div class="col-lg-4 col-md-5 col-sm-4 offset-md-1 offset-sm-1 col-12 mt-4">
</div>
</div>
</div>
</section>
</div>
Please tell me how to retrieve comments on a specific post by the post id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论