示例#1
0
        //
        // GET: /Answer/
        public ActionResult AnswerIndex(string id)
        {
            TempData["QuestionRef"] = id;
            int count = 1;
            IList <AnswerListModel> models = new ListStack <AnswerListModel>();
            var context = new StackOverflowContext();
            var ans     = context.Answers.Include(r => r.Owner).Include(r => r.QuestionReference);

            ans = ans.OrderByDescending(x => x.IsBestAnswer).ThenByDescending(y => y.Votes)
                  .ThenByDescending(z => z.CreationDate);

            foreach (Answer q in ans)
            {
                if (Guid.Parse(id) != q.QuestionReference.Id)
                {
                    continue;
                }

                var model = new AnswerListModel();

                model.AnswerCount = "Answer " + (count++);
                if (q.IsBestAnswer)
                {
                    model.BestAnswer = "Best Answer";
                }
                else
                {
                    model.BestAnswer = "";
                }
                model.CreationDate    = q.CreationDate;
                model.OwnerName       = q.Owner.Name;
                model.Votes           = q.Votes;
                model.OwnerId         = q.Owner.Id;
                model.AnswerId        = q.Id;
                model.QuestionId      = q.QuestionReference.Id;
                model.AnswerText      = q.AnswerText;
                model.QuestionOwnerId = q.QuestionReference.Owner.Id;
                model.UserHasVoted    = AnswerHasBeenVoted(model.AnswerId);
                models.Add(model);
            }

            context.SaveChanges();
            return(PartialView(models));
        }
示例#2
0
        public ActionResult RecentAnswers(Guid ownerId)
        {
            IList <AnswerListModel> models = new ListStack <AnswerListModel>();
            var context = new StackOverflowContext();
            var ans     = context.Answers.Include(r => r.Owner).Include(r => r.QuestionReference);

            ans = ans.OrderByDescending(z => z.CreationDate);
            int cont = 0;

            foreach (Answer q in ans)
            {
                if (ownerId != q.Owner.Id)
                {
                    continue;
                }
                if (cont >= 5)
                {
                    break;
                }
                var model = new AnswerListModel();
                if (q.IsBestAnswer)
                {
                    model.BestAnswer = "Best Answer";
                }
                else
                {
                    model.BestAnswer = "";
                }
                model.CreationDate    = q.CreationDate;
                model.OwnerName       = q.Owner.Name;
                model.Votes           = q.Votes;
                model.OwnerId         = q.Owner.Id;
                model.AnswerId        = q.Id;
                model.QuestionId      = q.QuestionReference.Id;
                model.AnswerText      = q.AnswerText;
                model.QuestionOwnerId = q.QuestionReference.Owner.Id;
                models.Add(model);
                cont++;
            }
            return(PartialView(models));
        }