public ActionResult Index(int qid, int page, AnswerShowViewModel model) { Answer ans = model.AnswerToAdd; UserStore<User> userStore = new UserStore<User>(repository.Context); UserManager<User> userManager = new UserManager<User>(userStore); ans.User = userManager.FindByName(HttpContext.User.Identity.Name); ans.QuestionId = qid; ans.Question = repository.Questions.SingleOrDefault(q => q.Id == ans.QuestionId); ans.PublishedAt = DateTime.Now; if (ModelState.IsValid) { repository.AnswerAdd(ans); TempData["AnswerAddSuccess"] = true; return RedirectToAction("Index", "Answer"); } return Index(qid, page); }
public ActionResult Index(int qid = 1, int page = 1) { ViewBag.OnAnswer = true; AnswerShowViewModel answerShowViewModel = new AnswerShowViewModel { Answers = repository.Answers .Where(a => a.QuestionId == qid) .OrderByDescending(a => a.PublishedAt) .Skip((page - 1) * PageSize) .Take(PageSize), PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = PageSize, TotalItems = repository.Answers .Where(a => a.QuestionId == qid) .Count() }, AnswerToAdd = new Answer() }; answerShowViewModel.AnswerToAdd.QuestionId = qid; answerShowViewModel.AnswerToAdd.Question = repository.Questions.SingleOrDefault(q => q.Id == qid); return View(answerShowViewModel); }