[Lập Trình] Tạo comment cho bài viết sử dụng ngôn ngữ asp.net

9/15/2020 1:39 PM | Lập trình

Website bạn tạo ra cần nhiều tương tác. Thì phần bình luận bài viết, sản phẩm là không thể thiếu. Bài viết này sẽ giới thiệu chi tiết cách tạo hệ thống bình luận cho một website asp.net mvc. Có cả demo nhé.

In this post, we are going to:

  • Create database.
  • Create MVC application.
  • Configuring entity framework ORM to connect to the database.
  • Create our Comments Controller.
  • Create Razor pages in order to build our application.

SQL Database part

Here, you find the scripts to create database and tables.

Create Database

 
  1. USE [master]  
  2. GO  
  3.   
  4. /****** Object:  Database [DBComments]    Script Date: 11/20/2017 2:23:19 PM ******/  
  5. CREATE DATABASE [DBComments]  
  6.  CONTAINMENT = NONE  
  7.  ON  PRIMARY   
  8. NAME = N'DBComments', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DBComments.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )  
  9.  LOG ON   
  10. NAME = N'DBComments_log', FILENAME = N'c:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\DBComments_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)  
  11. GO  
  12.   
  13. ALTER DATABASE [DBComments] SET COMPATIBILITY_LEVEL = 110  
  14. GO  
  15.   
  16. IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))  
  17. begin  
  18. EXEC [DBComments].[dbo].[sp_fulltext_database] @action = 'enable'  
  19. end  
  20. GO  
  21.   
  22. ALTER DATABASE [DBComments] SET ANSI_NULL_DEFAULT OFF   
  23. GO  
  24.   
  25. ALTER DATABASE [DBComments] SET ANSI_NULLS OFF   
  26. GO  
  27.   
  28. ALTER DATABASE [DBComments] SET ANSI_PADDING OFF   
  29. GO  
  30.   
  31. ALTER DATABASE [DBComments] SET ANSI_WARNINGS OFF   
  32. GO  
  33.   
  34. ALTER DATABASE [DBComments] SET ARITHABORT OFF   
  35. GO  
  36.   
  37. ALTER DATABASE [DBComments] SET AUTO_CLOSE OFF   
  38. GO  
  39.   
  40. ALTER DATABASE [DBComments] SET AUTO_CREATE_STATISTICS ON   
  41. GO  
  42.   
  43. ALTER DATABASE [DBComments] SET AUTO_SHRINK OFF   
  44. GO  
  45.   
  46. ALTER DATABASE [DBComments] SET AUTO_UPDATE_STATISTICS ON   
  47. GO  
  48.   
  49. ALTER DATABASE [DBComments] SET CURSOR_CLOSE_ON_COMMIT OFF   
  50. GO  
  51.   
  52. ALTER DATABASE [DBComments] SET CURSOR_DEFAULT  GLOBAL   
  53. GO  
  54.   
  55. ALTER DATABASE [DBComments] SET CONCAT_NULL_YIELDS_NULL OFF   
  56. GO  
  57.   
  58. ALTER DATABASE [DBComments] SET NUMERIC_ROUNDABORT OFF   
  59. GO  
  60.   
  61. ALTER DATABASE [DBComments] SET QUOTED_IDENTIFIER OFF   
  62. GO  
  63.   
  64. ALTER DATABASE [DBComments] SET RECURSIVE_TRIGGERS OFF   
  65. GO  
  66.   
  67. ALTER DATABASE [DBComments] SET  DISABLE_BROKER   
  68. GO  
  69.   
  70. ALTER DATABASE [DBComments] SET AUTO_UPDATE_STATISTICS_ASYNC OFF   
  71. GO  
  72.   
  73. ALTER DATABASE [DBComments] SET DATE_CORRELATION_OPTIMIZATION OFF   
  74. GO  
  75.   
  76. ALTER DATABASE [DBComments] SET TRUSTWORTHY OFF   
  77. GO  
  78.   
  79. ALTER DATABASE [DBComments] SET ALLOW_SNAPSHOT_ISOLATION OFF   
  80. GO  
  81.   
  82. ALTER DATABASE [DBComments] SET PARAMETERIZATION SIMPLE   
  83. GO  
  84.   
  85. ALTER DATABASE [DBComments] SET READ_COMMITTED_SNAPSHOT OFF   
  86. GO  
  87.   
  88. ALTER DATABASE [DBComments] SET HONOR_BROKER_PRIORITY OFF   
  89. GO  
  90.   
  91. ALTER DATABASE [DBComments] SET RECOVERY SIMPLE   
  92. GO  
  93.   
  94. ALTER DATABASE [DBComments] SET  MULTI_USER   
  95. GO  
  96.   
  97. ALTER DATABASE [DBComments] SET PAGE_VERIFY CHECKSUM    
  98. GO  
  99.   
  100. ALTER DATABASE [DBComments] SET DB_CHAINING OFF   
  101. GO  
  102.   
  103. ALTER DATABASE [DBComments] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )   
  104. GO  
  105.   
  106. ALTER DATABASE [DBComments] SET TARGET_RECOVERY_TIME = 0 SECONDS   
  107. GO  
  108.   
  109. ALTER DATABASE [DBComments] SET  READ_WRITE   
  110. GO  

Create Tables

After creating a database, we will move to create all the needed tables.

Users Table 

  1. USE [DBComments]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[Users]    Script Date: 11/20/2017 2:24:16 PM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[Users](  
  15.     [UserID] [int] IDENTITY(1,1) NOT NULL,  
  16.     [Username] [varchar](50) NULL,  
  17.     [imageProfile] [varchar](50) NULL,  
  18.  CONSTRAINT [PK_Users] PRIMARY KEY CLUSTERED   
  19. (  
  20.     [UserID] ASC  
  21. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  22. ON [PRIMARY]  
  23.   
  24. GO  
  25.   
  26. SET ANSI_PADDING OFF  
  27. GO  

Posts Table

 
  1. USE [DBComments]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[Posts]    Script Date: 11/20/2017 2:24:42 PM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[Posts](  
  15.     [PostID] [int] IDENTITY(1,1) NOT NULL,  
  16.     [Message] [varchar](50) NULL,  
  17.     [PostedDate] [datetime] NULL,  
  18.  CONSTRAINT [PK_Posts] PRIMARY KEY CLUSTERED   
  19. (  
  20.     [PostID] ASC  
  21. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  22. ON [PRIMARY]  
  23.   
  24. GO  
  25.   
  26. SET ANSI_PADDING OFF  
  27. GO  

Comments Table

 
  1. USE [DBComments]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[Comments]    Script Date: 11/20/2017 2:25:03 PM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[Comments](  
  15.     [ComID] [int] IDENTITY(1,1) NOT NULL,  
  16.     [CommentMsg] [varchar](maxNULL,  
  17.     [CommentedDate] [datetime] NULL,  
  18.     [PostID] [intNULL,  
  19.     [UserID] [intNULL,  
  20.  CONSTRAINT [PK_Comments] PRIMARY KEY CLUSTERED   
  21. (  
  22.     [ComID] ASC  
  23. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  24. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  25.   
  26. GO  
  27.   
  28. SET ANSI_PADDING OFF  
  29. GO  
  30.   
  31. ALTER TABLE [dbo].[Comments]  WITH CHECK ADD  CONSTRAINT [FK_Comments_Users] FOREIGN KEY([PostID])  
  32. REFERENCES [dbo].[Posts] ([PostID])  
  33. GO  
  34.   
  35. ALTER TABLE [dbo].[Comments] CHECK CONSTRAINT [FK_Comments_Users]  
  36. GO  
  37.   
  38. ALTER TABLE [dbo].[Comments]  WITH CHECK ADD  CONSTRAINT [FK_Comments_Users1] FOREIGN KEY([UserID])  
  39. REFERENCES [dbo].[Users] ([UserID])  
  40. GO  
  41.   
  42. ALTER TABLE [dbo].[Comments] CHECK CONSTRAINT [FK_Comments_Users1]  
  43. GO  

SubComments Table

 
  1. USE [DBComments]  
  2. GO  
  3.   
  4. /****** Object:  Table [dbo].[SubComments]    Script Date: 11/20/2017 2:25:29 PM ******/  
  5. SET ANSI_NULLS ON  
  6. GO  
  7.   
  8. SET QUOTED_IDENTIFIER ON  
  9. GO  
  10.   
  11. SET ANSI_PADDING ON  
  12. GO  
  13.   
  14. CREATE TABLE [dbo].[SubComments](  
  15.     [SubComID] [int] IDENTITY(1,1) NOT NULL,  
  16.     [CommentMsg] [varchar](50) NULL,  
  17.     [CommentedDate] [datetime] NULL,  
  18.     [ComID] [intNULL,  
  19.     [UserID] [intNULL,  
  20.  CONSTRAINT [PK_SubComments] PRIMARY KEY CLUSTERED   
  21. (  
  22.     [SubComID] ASC  
  23. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  24. ON [PRIMARY]  
  25.   
  26. GO  
  27.   
  28. SET ANSI_PADDING OFF  
  29. GO  
  30.   
  31. ALTER TABLE [dbo].[SubComments]  WITH CHECK ADD  CONSTRAINT [FK_SubComments_Comments] FOREIGN KEY([ComID])  
  32. REFERENCES [dbo].[Comments] ([ComID])  
  33. GO  
  34.   
  35. ALTER TABLE [dbo].[SubComments] CHECK CONSTRAINT [FK_SubComments_Comments]  
  36. GO  
  37.   
  38. ALTER TABLE [dbo].[SubComments]  WITH CHECK ADD  CONSTRAINT [FK_SubComments_Users] FOREIGN KEY([UserID])  
  39. REFERENCES [dbo].[Users] ([UserID])  
  40. GO  
  41.   
  42. ALTER TABLE [dbo].[SubComments] CHECK CONSTRAINT [FK_SubComments_Users]  
  43. GO  
Create your MVC application

Open Visual Studio and select File >> New Project.

The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.

 

Next, a new dialog will pop up for selecting the template. We are going to choose MVC template and click OK.

 
 
Once our project is created, we will add ADO.NET Entity Data Model.

Adding ADO.NET Entity Data Model

Right-click on the project name, click Add >> Add New Item.

A dialog box will pop up. Inside Visual C#, select Data >> ADO.NET Entity Data Model, and enter the name for your DbContext model as SysComments, then click Add.

 
 
As you can see, we have 4 model contents. We are selecting the first approach (EF Designer from database).
 
 
 
As you can see below, we need to select server name, then via drop-down list connected to a database section, you must choose your database name and finally click OK.
 
 
 
For the next step, the dialog Entity Data Model Wizard will pop up for choosing objects which will be used in our application. We are selecting all the tables except sysdiagrams and click Finish.
 
Finally, we can see that EDMX model generates all objects, as shown below.
 
 
 

Create a Controller

Now, we are going to create a Controller. Right-click on the Controllers folder> > Add >> Controller>> selecting MVC 5 Controller - Empty>> click Add. In the next dialog, name the controller CommentsController and then click Add.

 
 
 
  
CommentsController.cs

Here, we have used Get Users action in order to authenticate user by providing her/his username. If the user exists in users table, we will redirect him to Get Posts action, otherwise the following message ‘username does not exist’ will be displayed.

 
  1. [HttpGet]  
  2.         public ActionResult GetUsers()  
  3.         {  
  4.             return View();  
  5.         }  
  6.   
  7.         [HttpPost]  
  8.         public ActionResult GetUsers(string username)  
  9.         {  
  10.             User user = dbContext.Users.Where(u => u.Username.ToLower() == username.ToLower())  
  11.                                  .FirstOrDefault();  
  12.   
  13.             if(user != null)  
  14.             {  
  15.                 Session["UserID"] = user.UserID;  
  16.                 return RedirectToAction("GetPosts");  
  17.             }  
  18.   
  19.             ViewBag.Msg = "Username does not exist !";  
  20.             return View();  
  21.         }  

GetUsers.cshtml

 
  1. @{  
  2.     ViewBag.Title = "GetUsers";  
  3. }  
  4.   
  5.   
  6. @using (Html.BeginForm("GetUsers""Comments", FormMethod.Post))  
  7. {  
  8.     <div class="blockUser" style="width: 44%; margin: 16% 26%;">  
  9.   
  10.         <div style="margin-left: 23%;">  
  11.             <input type="text" class="form-control input-lg" name="username" placeholder="Username" /><br />  
  12.             <input type="submit" class="btn btn-success btn-lg" value="Let's GO ^_^" style="width: 75%;" />  
  13.         </div>  
  14.   
  15.     </div>  
  16.   
  17. }  
  18.   
  19. @if (ViewBag.Msg != null)  
  20. {  
  21.     <div class="alert alert-danger" role="alert">Oups! @ViewBag.Msg</div>  
  22. }  

In order to display all posts, we have created Get Posts action which selects all posts from posts table, then displayed them within GetPosts.cshtml by using foreach loop.

 
  1. [HttpGet]  
  2. public ActionResult GetPosts()  
  3. {  
  4.     IQueryable<PostsVM> Posts = dbContext.Posts  
  5.                                          .Select(p => new PostsVM  
  6.                                          {  
  7.                                              PostID = p.PostID,  
  8.                                              Message = p.Message,  
  9.                                              PostedDate = p.PostedDate.Value  
  10.                                          }).AsQueryable();  
  11.   
  12.     return View(Posts);  
  13. }  

GetPosts.cshtml

 
  1. @model IQueryable<CommentsSystemMVC5.ViewModels.PostsVM>  
  2.   
  3. @{  
  4.     ViewBag.Title = "GetPosts";  
  5. }  
  6.   
  7. @if (Model != null)  
  8. {  
  9.     foreach (var post in Model)  
  10.     {  
  11.         <div class="panel panel-default" style="width: 80%;">  
  12.             <div class="panel-body">  
  13.                 <div class="avatar">  
  14.                     <img src="~/Images/avatar.png" class="img-circle" style="width: 60px;"/>  
  15.                     <span> <a href="" style="font-weight:bold">Leo Messi</a> </span><br />  
  16.                     <p style="margin-left: 60px; margin-top: -19px;">  
  17.                         <span class="glyphicon glyphicon-time" aria-hidden="true"></span>  
  18.                         <time class="timeago" datetime="@post.PostedDate">@post.PostedDate</time>  
  19.                     </p>  
  20.                       
  21.                 </div>  
  22.   
  23.                 <div class="postMessage" style="margin-top: 11px; margin-left: 9px;">  
  24.                     <span class="label label-warning"> @string.Format("Post #{0}", post.PostID) </span><br />  
  25.                     <p class="message">  
  26.                         @post.Message  
  27.                     </p>  
  28.                 </div>  
  29.                   
  30.             </div>  
  31.             <div class="panel-footer">    
  32.                     <button type="button"  class="btn btn-default Comment" data-id="@post.PostID" value="Comment">   
  33.                         <span class="glyphicon glyphicon-comment" aria-hidden="true"></span> Comment  
  34.                     </button>  
  35.    
  36.            </div>  
  37.            <div id="@string.Format("{0}_{1}","commentsBlock", post.PostID)" style="border: 1px solid #f1eaea; background-color: #eaf2ff;">  
  38.   
  39.                <div class="AddComment" style="margin-left: 30%;  margin-bottom: 5px; margin-top: 8px;">  
  40.                    <input type="text" id="@string.Format("{0}_{1}", "comment", post.PostID)" class="form-control"  placeholder="Add a Comment ..."  style="display: inline;" />  
  41.                    <button type="button" class="btn btn-default addComment" data-id="@post.PostID"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>  
  42.                </div>  
  43.   
  44.            </div>  
  45.               
  46.               
  47.   
  48.         </div>  
  49.     }  
  50. }  
  51.   
  52. @section Scripts  
  53. {  
  54.       
  55.     <script type="text/javascript">  
  56.   
  57.         $(document).ready(function () {  
  58.   
  59.               
  60.             //Click Comment  
  61.             $('.Comment').on('click', function () {  
  62.   
  63.                 var id = $(this).attr("data-id");  
  64.                 var allCommentsArea = $('<div>').addClass('allComments_' + id);  
  65.   
  66.                 //function that allow us to get all comments related to post id  
  67.                 $.ajax({  
  68.   
  69.                     type: 'GET',  
  70.                     url: '@Url.Action("GetComments", "Comments")',  
  71.                     data: { postId: id },  
  72.                     success: function (response) {  
  73.   
  74.                         if ($('div').hasClass('allComments_' + id + ''))  
  75.                         {  
  76.                             $('div[class=allComments_' + id + ']').remove();  
  77.                         }  
  78.                         //console.log(response);  
  79.                           
  80.                           
  81.                         allCommentsArea.html(response);  
  82.                         allCommentsArea.prependTo('#commentsBlock_' + id);  
  83.   
  84.   
  85.                     },  
  86.                     error: function (response) {  
  87.                         alert('Sorry: Comments cannot be loaded !');  
  88.                     }  
  89.   
  90.   
  91.                 })  
  92.   
  93.             });  
  94.   
  95.             //Add New Comment  
  96.             $('.addComment').on('click', function () {  
  97.   
  98.                 var postId = $(this).attr('data-id');  
  99.                 var commentMsg = $('#comment_' + postId).val();  
  100.                 var dateTimeNow = new Date();  
  101.                   
  102.                 //alert('Hello');  
  103.                 var comment = {  
  104.                     CommentMsg: commentMsg,  
  105.                     CommentedDate: dateTimeNow.toLocaleString()  
  106.                 };  
  107.   
  108.                 $.ajax({  
  109.   
  110.                     type: 'POST',  
  111.                     url: '@Url.Action("AddComment", "Comments")',  
  112.                     data: { comment, postId },  
  113.                     success: function (response) {  
  114.   
  115.                         $('div[class=allComments_' + postId + ']').remove();  
  116.   
  117.                         var allCommentsArea = $('<div>').addClass('allComments_' + postId);  
  118.                         allCommentsArea.html(response);  
  119.   
  120.                         allCommentsArea.prependTo('#commentsBlock_' + postId);  
  121.                          
  122.                     },  
  123.                     error: function (response) {  
  124.                         alert('Sorry: Something Wrong');  
  125.                     }  
  126.   
  127.                 });