public ActionResult VoteQuestion(int id, string type) { if (Session["UserID"] == null) { return(RedirectToAction("Index", "Home")); } string UserID = Session["UserID"].ToString(); QuestionVotes vote = new QuestionVotes { UserID = UserID, PostID = id, Type = type }; _postsServices.VoteQuestion(vote); return(RedirectToAction("ViewPost", "Posts", new { id })); }
// Vote a question public void VoteQuestion(QuestionVotes QV) { QuestionVotes OldQV = _dbContext.QuestionVotes.Where(q => q.PostID == QV.PostID && q.UserID == QV.UserID).FirstOrDefault(); if (OldQV != null) { if (OldQV.Type == QV.Type) { return; } else { _dbContext.QuestionVotes.Remove(OldQV); _dbContext.QuestionVotes.Add(QV); } } else { _dbContext.QuestionVotes.Add(QV); } _dbContext.SaveChanges(); }