Home > Net >  how to add comment to post by using id in asp.net mvc?
how to add comment to post by using id in asp.net mvc?

Time:06-28

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}$", 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.

CodePudding user response:

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:

    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)

Here is a link to Microsoft documentation that may help you in the future.

  • Related