//给问题增加回答 public void addAnswerToQuestion(int UID, int QID, Answer answer) { answer.AQuestionID = QID; answer.AUserID = UID; answer.AStatus = "unknown"; if (answer.AContent == null) answer.AContent = ""; db.Answers.Add(answer); Question question = db.Questions.Find(QID); question.QAnswerNum++; User user = db.Users.Find(UID); user.UAnswerNum++; db.SaveChanges(); }
public ActionResult answer(int id,Answer A) { int Uid = -1; if (Request.Cookies["id"] != null) { var cookies = Request.Cookies["id"].Value; if (cookies != null) Uid = Int32.Parse(cookies); } else return RedirectToAction("index", "index"); int QID = id; List<Answer> test = db.Answers.Where(a => a.AUserID == Uid).Where(a => a.AQuestionID == QID).ToList(); if (test.Count() == 0) { model.addAnswerToQuestion(Uid, id, A); return RedirectToAction("show", new { id = id }); } else { ViewBag.info = "您已经回答过该问题,不能再回答"; return RedirectToAction("show", new { id = id }); } }
//获取综合评价最好的某个问题的答案 public Answer getHotAnswerByQuestionID(int id) { List<Answer> answer = db.Answers.Where(a => a.AQuestionID == id).ToList(); Answer HotAnswer = new Answer(); if (answer.Count() == 0) { HotAnswer.ATime = db.Questions.Find(id).QTime; HotAnswer.AQuestionID = id; HotAnswer.AStatus = "unknown"; HotAnswer.AContent = " "; HotAnswer.AUserID = 0; } else { HotAnswer = answer[0]; int evaluation = 0; for (int i = 1; i < answer.Count(); i++) { evaluation = answer[i].AUpvoteNum - answer[i].ADownvoteNum - answer[i].AUselessNum; if (evaluation >= HotAnswer.AUpvoteNum - HotAnswer.ADownvoteNum - HotAnswer.AUselessNum) { HotAnswer = answer[i]; } } } return HotAnswer; }