简体   繁体   English

如何在asp.net mvc中使用id添加评论?

[英]how to add comment to post by using id in asp.net mvc?

developer i am a beginner of asp.net mvc .I want to add comment to the post by using Post Id.开发人员我是 asp.net mvc 的初学者。我想使用 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}$", 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.请推荐一个添加评论的好方法。

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:像这样,您可以使用连接的 PostId 向数据库添加评论,框架将能够填充帖子对象的评论列表:

Post:邮政:

    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; }

        public IEnumerable<Comment> Comments { get; set; }
    }

Comment:评论:

    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; }
    }

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)如果您想这样做,请不要忘记包含评论表 db.Post.Include(post => post.Comments)

Here is a link to Microsoft documentation that may help you in the future. 是 Microsoft 文档的链接,将来可能会对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM