Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Guys, I have a forum and I want users to be able to reply comments so that the reply would appear with the Comment.

This is what I Have:

C#
public ActionResult Quote(int? id)
       {
           var user = User.Identity.Name;
           var userId = WebSecurity.CurrentUserId;
           //var userId = db.UserProfiles.FirstOrDefault(x => x.UserName == user).UserId;
           if (User.Identity.Name == null)
           {
               return RedirectToAction("Login", "Account");
           }
           if (id == null)
           {
               return RedirectToAction("topics", "forum");
           }

           //var p = db.Posts.FirstOrDefault(x => x.PostId == id.Value);
           var post = db.Posts.Find(id.Value);

           var comment = db.UserComments.FirstOrDefault();
           var usercomment = db.UserComments.Find(comment.UserCommentId);

           if (usercomment == null)
           {
               return RedirectToAction("topics");
           }

           return View(new Quote { UserId = WebSecurity.CurrentUserId, PostId = id.Value, UserCommentId = usercomment.UserCommentId });
       }


       [HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Quote(Quote quote, int id = 0)
       {

           if (ModelState.IsValid)
           {
               if (Request.Files.Count > 0)
               {
                   System.Random randomInteger = new System.Random();
                   int genNumber = randomInteger.Next(1000000);
                   HttpPostedFileBase file = Request.Files[0];
                   if (file.ContentLength > 0 && file.ContentType.ToUpper().Contains("JPEG") || file.ContentType.ToUpper().Contains("PNG") || file.ContentType.ToUpper().Contains("JPG"))
                   {
                       WebImage img = new WebImage(file.InputStream);
                       if (img.Width > 500)
                       {
                           img.Resize(width: 500, height: 400, preserveAspectRatio: true, preventEnlarge: true);
                       }

                       if (img.Height > 700)
                       {
                           img.Resize(width: 500, height: 400, preserveAspectRatio: true, preventEnlarge: true);
                       }
                       string fileName = Path.Combine(Server.MapPath("~/Uploads/"), Path.GetFileName(genNumber + file.FileName));
                       img.Save(fileName);
                       if (fileName == null)
                       {
                           ViewBag.image = "True";
                       }
                       quote.QuoteFile = fileName;
                   }
               }
               var user = WebSecurity.CurrentUserId;
               //var userId = db.UserProfiles.FirstOrDefault(a => a.UserId == user);
               db.Quotes.Add(quote);
               quote.QuotedBy = User.Identity.Name;
               var post = db.Quotes.Include(f => f.Posts).Where(x=>x.UserCommentId == quote.UserCommentId).OrderByDescending(s => s.QuoteId);

               quote.DateQuoted = DateTime.Now;
               db.SaveChanges();
               return RedirectToAction("topics", "forum", new { id = quote.PostId});
           }

           return View(quote);
       }

       public ActionResult _Quote(int id = 0)
       {
           //var comment = db.UserComments.FirstOrDefault(x => x.PostId == id);
           var user = WebSecurity.CurrentUserId;
           var comments = db.Quotes.Where(x => x.PostId == id).ToList();
           //var quotes = db.UserComments.Where(x => x.PostId == id).ToList();

           var p = new UserComment();
           p.DateUserCommented = DateTime.Now;
           DateTime dt = Convert.ToDateTime(p.DateUserCommented);
           string strDate = dt.ToString("dd MMMM yyyy - hh:mm tt");
           ViewBag.p = strDate;

           ViewBag.data = comments;

           return PartialView(comments);
       }
Posted
Comments
Suvendu Shekhar Giri 22-Dec-15 9:32am    
..and what is the problem?
Is it throwing any exception/error?
Member 12139499 22-Dec-15 10:28am    
its working fine but its not picking the particular comment content, its picking the !st comment in that Post. I think its because of the FirstOrDefault(), but i dont know how to query it. Can u please help me out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900