如何通过在ASP.NET MVC中使用ID添加评论以发布?
开发人员我是ASP.NET MVC的初学者。我想使用POST ID向帖子中添加评论。 这是评论和发布模型 在这里发表
namespace Post.Models
{
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string SubTitle { get; set; }
public string Content { get; set; }
public string Date { get; set; }
public string Image { get; set; }
public string Slug { get; set; }
}
}
评论
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 IActionResult AddComment()
{
return View();
}
[HttpPost]
public IActionResult AddComment(Comments mycomment)
{
db.Tbl_Comments.Add(mycomment);
db.SaveChanges();
return RedirectToAction("Post", "Home");
}
[Route("Home/Post/{Slug}")]
public IActionResult Post(string Slug)
{
SharedLayOutData();
var DetailedPost = db.Tbl_Post.Where(x => x.Slug == Slug).FirstOrDefault();
return View(DetailedPost);
}
,请推荐一种添加评论的好方法。
developer i am a beginner of asp.net mvc .I want to add comment to the post by using Post Id.
here is comment and post model
Post
namespace Post.Models
{
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string SubTitle { get; set; }
public string Content { get; set; }
public string Date { get; set; }
public string Image { get; set; }
public string Slug { get; set; }
}
}
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();
}
}
here is controller code
public IActionResult AddComment()
{
return View();
}
[HttpPost]
public IActionResult AddComment(Comments mycomment)
{
db.Tbl_Comments.Add(mycomment);
db.SaveChanges();
return RedirectToAction("Post", "Home");
}
[Route("Home/Post/{Slug}")]
public IActionResult Post(string Slug)
{
SharedLayOutData();
var DetailedPost = db.Tbl_Post.Where(x => x.Slug == Slug).FirstOrDefault();
return View(DetailedPost);
}
please recommend a good way to add comment please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在对象之间建立关系应解决您的问题。这样,您可以使用Connected PostID添加评论,并且该框架将能够填写帖子对象上的注释列表:
post:
评论:
另一种方法是通过更改对该的评论列表这样的帖子您可以一次添加,删除或编辑多个注释。如果您想这样做,请不要忘记包括注释表DB.Post.include(post => post.comments)
在这里是Microsoft文档的链接,将来可能会为您提供帮助。
Creating a relationship between the objects should solve your issue. Like this you can add a comment to the database with the connected PostId and the framework will be able to fill the list of comments on the post object:
Post:
Comment:
An other way of doing this is by altering the list of Comments on the Post like that you can add, remove or edit multiple comments at once. If you want to do it like that, do not forget to include the Comment table db.Post.Include(post => post.Comments)
Here is a link to Microsoft documentation that may help you in the future.