如何通过在ASP.NET MVC中使用ID添加评论以发布?

发布于 2025-02-10 19:47:12 字数 1653 浏览 1 评论 0原文

开发人员我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愿得七秒忆 2025-02-17 19:47:12

在对象之间建立关系应解决您的问题。这样,您可以使用Connected 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; }
    }

评论:

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

另一种方法是通过更改对该的评论列表这样的帖子您可以一次添加,删除或编辑多个注释。如果您想这样做,请不要忘记包括注释表DB.Post.include(post =&gt; 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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文