public ActionResult Create(Comment comment) { if (ModelState.IsValid) { comment.Author = User.Identity.Name; cdb.Comments.Add(comment); cdb.SaveChanges(); Post post = pdb.Posts.Find(comment.RefferedPost); IntegerData idata = new IntegerData(); idata.Data = comment.ID; post.Comments.Add(idata); pdb.Entry(post).State = EntityState.Modified; pdb.SaveChanges(); //if is hitchhiker if (comment.Hitchhiker == true) { Hitchhiker hitch = new Hitchhiker() { Name = comment.Author, Phone = comment.PhoneNumber, PostID = comment.RefferedPost }; hdb.Hitchhikers.Add(hitch); hdb.SaveChanges(); } return(RedirectToAction("Details", "Post", new { id = post.ID })); } else { return(View(comment)); } }
public ActionResult Edit(Comment comment) { if (ModelState.IsValid) { Hitchhiker h1 = hdb.Hitchhikers.Where(h => h.Name == comment.Author).SingleOrDefault(); if (comment.Hitchhiker == false) { if (h1 != null) { hdb.Hitchhikers.RemoveRange(hdb.Hitchhikers.Where(c => c.PostID == h1.PostID)); } } else { if (h1 != null) { h1.Phone = comment.PhoneNumber; hdb.Entry(h1).State = EntityState.Modified; } else { h1 = new Hitchhiker() { Name = comment.Author, Phone = comment.PhoneNumber, PostID = comment.RefferedPost }; hdb.Hitchhikers.Add(h1); } } hdb.SaveChanges(); cdb.Entry(comment).State = EntityState.Modified; cdb.SaveChanges(); return(RedirectToAction("Index", "Users")); } return(RedirectToAction("Index", "Users")); }