Comments System With ASP.NET MVC 5 And jQuery

Introduction

A comments system has become very important in order to share our opinion on social media and forums with others.

So, in this article, we will implement comments system by using ASP.NET MVC 5 and jQuery, Bootstrap. I hope you will like this.

Prerequisites

Make sure you have installed Visual Studio 2015 (.NET Framework 4.5.2) and SQL Server.

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.                 });  
  128.   
  129.             });  
  130.   
  131.             jQuery("time.timeago").timeago();  
  132.   
  133.         });  
  134.   
  135.     </script>  
  136.           
  137.       
  138. }  

As you can see, Get Comments action is responsible to get all comments related to the given post id, then we will display them within _MyComments partial view.

  1. public PartialViewResult GetComments(int postId)  
  2.         {  
  3.             IQueryable<CommentsVM> comments = dbContext.Comments.Where(c => c.Post.PostID == postId)  
  4.                                      .Select(c => new CommentsVM  
  5.                                       {  
  6.                                          ComID = c.ComID,  
  7.                                          CommentedDate = c.CommentedDate.Value,  
  8.                                          CommentMsg = c.CommentMsg,  
  9.                                          Users = new UserVM  
  10.                                          {  
  11.                                             UserID = c.User.UserID,  
  12.                                             Username = c.User.Username,  
  13.                                             imageProfile = c.User.imageProfile  
  14.                                          }  
  15.                                        }).AsQueryable();  
  16.   
  17.             return PartialView("~/Views/Shared/_MyComments.cshtml", comments);  
  18.         }  

Shared/_MyComments.cshtml

To add partial view, from the Solution Explorer, expand Views folder, right click on Shared folder >> Add >> View. Don’t forget to check "Create as a partial view" option.

  1. @model   IQueryable<CommentsSystemMVC5.ViewModels.CommentsVM>  
  2. @using CommentsSystemMVC5.ViewModels;  
  3.   
  4.   
  5.     @if (Model != null)  
  6.     {  
  7.   
  8.         foreach (CommentsVM comment in Model)  
  9.         {  
  10.             <div class="row" style="width: 100.3%; border-bottom: 1px solid #d2cece; margin-right: -14px; margin-left: -1px;">  
  11.   
  12.                 <div class="col-md-4" style="width: 21%;">  
  13.                     <div class="userProfil" style="margin-left: 9px; margin-top: 12px;">  
  14.                         <img src="~/Images/@comment.Users.imageProfile" class="img-circle" style="width: 46px; height: 53px; border: 1px solid #bcb8b8;" />  
  15.                         <a href="#" style="margin-left: 5px; font-weight: bold; font-size: 13px;"> @comment.Users.Username </a>  
  16.                     </div>  
  17.                 </div>  
  18.   
  19.                 <div class="col-md-7" style="width: 60%;">  
  20.                     <div class="commentDetails">  
  21.                         <p style="margin-top: 27px; font-size: 13px; color: #9c9898;"> @comment.CommentMsg </p>  
  22.                         <a href="#" class="Reply" data-id="@comment.ComID">Reply</a>  
  23.                         <div class="@string.Format("{0}_{1}", "ReplayComments", comment.ComID)" style="display:none;">  
  24.   
  25.   
  26.                             <div class="ReplayCommentInput" style="margin-left: 3%;  margin-bottom: 5px; margin-top: 8px;">  
  27.                                 <input type="text" id="@string.Format("{0}_{1}", "inputReplay", comment.ComID)" class="form-control"  placeholder="Add a Comment ..." style="display: inline;" />  
  28.                                 <button type="button" class="btn btn-default ReplyAddComment" data-id="@comment.ComID"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span></button>  
  29.                             </div>  
  30.   
  31.                         </div>  
  32.   
  33.                     </div>  
  34.                 </div>  
  35.   
  36.                 <div class="col-md-1" style="width: 19%;">  
  37.                     <div class="commentDate">  
  38.                         <span class="glyphicon glyphicon-time" aria-hidden="true"></span>  
  39.                         <time class="timeago" style="margin-top: 27px; font-size: 13px; color: #9c9898;  margin-left: 4px;" datetime="@comment.CommentedDate">@comment.CommentedDate</time>  
  40.     
  41.                     </div>  
  42.                 </div>  
  43.             </div>  
  44.   
  45.         }  
  46.   
  47.   
  48.     }  
  49.   
  50.   
  51. <script type="text/javascript">  
  52.   
  53.     $(document).ready(function () {  
  54.   
  55.         //Get All ReplyComment  
  56.         $('.Reply').on('click', function () {  
  57.   
  58.             var ComID = $(this).attr('data-id');  
  59.               
  60.             $.ajax({  
  61.   
  62.                 type: 'GET',  
  63.                 url: '@Url.Action("GetSubComments", "Comments")',  
  64.                 data: { ComID },  
  65.                 success: function (response) {  
  66.   
  67.                     if ($('div').hasClass('zoneReply_' + ComID + ''))  
  68.                     {  
  69.                         $('div [class=zoneReply_' + ComID + ']').remove();  
  70.                     }  
  71.   
  72.                     var selReply = $("<div>").addClass('zoneReply_' + ComID);  
  73.   
  74.                     selReply.append(response);  
  75.                     selReply.prependTo($('.ReplayComments_' + ComID));  
  76.   
  77.                     $('.ReplayComments_' + ComID).show();  
  78.   
  79.                 },  
  80.                 error: function (response) {  
  81.                     alert('something Wrong');  
  82.                 }  
  83.             });  
  84.   
  85.         });  
  86.   
  87.         //Add Reply Comment  
  88.         $('.ReplyAddComment').on('click', function () {  
  89.   
  90.             var ComID = $(this).attr('data-id');  
  91.             var CommentMsg = $('#inputReplay_' + ComID).val();  
  92.             var dateTimeNow = new Date();  
  93.   
  94.             var subComment = {  
  95.                 CommentMsg: CommentMsg,  
  96.                 CommentedDate: dateTimeNow.toLocaleString()  
  97.             };  
  98.               
  99.   
  100.             $.ajax({  
  101.   
  102.                 type: 'POST',  
  103.                 url: '@Url.Action("AddSubComment", "Comments")',  
  104.                 data: { subComment, ComID },  
  105.                 success: function (response) {  
  106.   
  107.                     if ($('div').hasClass('zoneReply_' + ComID + '')) {  
  108.                         $('div [class=zoneReply_' + ComID + ']').remove();  
  109.                     }  
  110.   
  111.                     var selReply = $("<div>").addClass('zoneReply_' + ComID);  
  112.   
  113.                     selReply.append(response);  
  114.                     selReply.prependTo($('.ReplayComments_' + ComID));  
  115.   
  116.                     $('.ReplayComments_' + ComID).show();  
  117.   
  118.                 },  
  119.                 error: function (response) {  
  120.                     alert('something Wrong');  
  121.                 }  
  122.             });  
  123.   
  124.         });  
  125.   
  126.         jQuery("time.timeago").timeago();  
  127.   
  128.   
  129.     })  
  130. </script>  

Adding a comment is very simple. We have created Add Comment action which accepts the comment and posts id parameters then persisting comment object into comments table by using add extension method.

  1. [HttpPost]  
  2.         public ActionResult AddComment(CommentsVM comment, int postId)  
  3.         {  
  4.             //bool result = false;  
  5.             Comment commentEntity = null;  
  6.             int userId = (int)Session["UserID"];  
  7.   
  8.             var user = dbContext.Users.FirstOrDefault(u => u.UserID == userId);  
  9.             var post = dbContext.Posts.FirstOrDefault(p => p.PostID == postId);  
  10.   
  11.             if (comment != null)  
  12.             {  
  13.                   
  14.                 commentEntity = new EDMX.Comment  
  15.                 {  
  16.                     CommentMsg = comment.CommentMsg,  
  17.                     CommentedDate = comment.CommentedDate,  
  18.                 };  
  19.                   
  20.   
  21.                 if (user != null && post != null)  
  22.                 {  
  23.                     post.Comments.Add(commentEntity);  
  24.                     user.Comments.Add(commentEntity);  
  25.   
  26.                     dbContext.SaveChanges();  
  27.                     //result = true;  
  28.                 }  
  29.             }  
  30.   
  31.             return RedirectToAction("GetComments""Comments"new { postId  = postId });  
  32.         }  

Here, Get subComments is used to select all sub comments related to the given comID (comment id). Then, we will display them within _MySubComments partial view.

  1. [HttpGet]  
  2.         public PartialViewResult GetSubComments(int ComID)  
  3.         {  
  4.             IQueryable<SubCommentsVM> subComments = dbContext.SubComments.Where(sc => sc.Comment.ComID == ComID)  
  5.                                        .Select(sc => new SubCommentsVM  
  6.                                         {  
  7.                                           SubComID = sc.SubComID,  
  8.                                           CommentMsg = sc.CommentMsg,  
  9.                                           CommentedDate = sc.CommentedDate.Value,  
  10.                                           User = new UserVM   
  11.                                           {  
  12.                                              UserID = sc.User.UserID,  
  13.                                              Username = sc.User.Username,  
  14.                                              imageProfile = sc.User.imageProfile  
  15.                                           }  
  16.                                         }).AsQueryable();  
  17.   
  18.             return PartialView("~/Views/Shared/_MySubComments.cshtml", subComments);  

  19.         }  

Shared/_MySubComments.cshtml

  1. @model IQueryable<CommentsSystemMVC5.ViewModels.SubCommentsVM>  
  2. @using CommentsSystemMVC5.ViewModels  
  3.   
  4.   
  5.     @if (Model != null)  
  6.     {  
  7.   
  8.         foreach (SubCommentsVM subComment in Model)  
  9.         {  
  10.             <div class="row" style="width: 100.3%; border-bottom: 1px solid #d2cece; margin-right: -14px; margin-left: -1px;">  
  11.   
  12.                 <div class="col-md-4" style="width: 36%;">  
  13.                     <div class="userProfil" style="margin-left: 9px; margin-top: 12px;">  
  14.                         <img src="~/Images/@subComment.User.imageProfile" class="img-circle" style="width: 46px; height: 53px; border: 1px solid #bcb8b8;" />  
  15.                         <a href="#" style="margin-left: 5px; font-weight: bold; font-size: 13px;"> @subComment.User.Username </a>  
  16.                     </div>  
  17.                 </div>  
  18.   
  19.                 <div class="col-md-7" style="width: 32%;">  
  20.                     <div class="commentDetails">  
  21.                         <p style="margin-top: 27px; font-size: 13px; color: #9c9898;"> @subComment.CommentMsg </p>  
  22.                     </div>  
  23.                 </div>  
  24.   
  25.                 <div class="col-md-1" style="width: 32%;">  
  26.                     <div class="commentDetails">  
  27.                         <span class="glyphicon glyphicon-time" aria-hidden="true"></span>  
  28.                         <time class="timeago" style="margin-top: 27px; font-size: 13px; color: #9c9898;  margin-left: 4px;" datetime="@subComment.CommentedDate">@subComment.CommentedDate</time>  
  29.   
  30.                     </div>  
  31.                 </div>  
  32.   
  33.             </div>  
  34.         }  
  35.     }  
  36.   
  37. <script type="text/javascript">  
  38.   
  39.     $(document).ready(function () {  
  40.   
  41.         jQuery("time.timeago").timeago();  
  42.     });  
  43.   
  44. </script>  
  1. [HttpPost]  
  2.         public ActionResult AddSubComment(SubCommentsVM subComment, int ComID)  
  3.         {  
  4.             SubComment subCommentEntity = null;  
  5.             int userId = (int)Session["UserID"];  
  6.   
  7.             var user = dbContext.Users.FirstOrDefault(u => u.UserID == userId);  
  8.             var comment = dbContext.Comments.FirstOrDefault(p => p.ComID == ComID);  
  9.   
  10.             if (subComment != null)  
  11.             {  
  12.   
  13.                 subCommentEntity = new EDMX.SubComment  
  14.                 {  
  15.                     CommentMsg = subComment.CommentMsg,  
  16.                     CommentedDate = subComment.CommentedDate,  
  17.                 };  
  18.   
  19.   
  20.                 if (user != null && comment != null)  
  21.                 {  
  22.                     comment.SubComments.Add(subCommentEntity);  
  23.                     user.SubComments.Add(subCommentEntity);  
  24.   
  25.                     dbContext.SaveChanges();  
  26.                     //result = true;  
  27.                 }  
  28.             }  
  29.   
  30.             return RedirectToAction("GetSubComments""Comments"new { ComID = ComID });  
  31.   
  32.         }  

View Model

Don’t forget to add the the following View Models.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace CommentsSystemMVC5.ViewModels  
  7. {  
  8.     public class PostsVM  
  9.     {  
  10.         public int PostID { getset; }  
  11.         public string Message { getset; }  
  12.         public DateTime PostedDate { getset; }  
  13.     }  
  14.   
  15.     public class CommentsVM  
  16.     {  
  17.         public int ComID { getset; }  
  18.         public string CommentMsg { getset; }  
  19.         public DateTime CommentedDate { getset; }  
  20.         public PostsVM Posts { getset; }  
  21.         public UserVM Users { getset; }  
  22.     }  
  23.   
  24.     public class UserVM  
  25.     {  
  26.         public int UserID { getset; }  
  27.         public string Username { getset; }  
  28.         public string imageProfile { getset; }  
  29.     }  
  30.   
  31.     public class SubCommentsVM  
  32.     {  
  33.         public int SubComID { getset; }  
  34.         public string CommentMsg { getset; }  
  35.         public DateTime CommentedDate { getset; }  
  36.         public CommentsVM Comment { getset; }  
  37.         public UserVM User { getset; }  
  38.     }  
  39.   
  40.   
  41. }  

Output

Now, our comments system application is ready. We can run and see the output in the browser.

 

That’s all. Please send your feedback and queries in comments box.


Similar Articles