public async Task <IActionResult> Questions(int pageNumber) { var model = new QuestionsModel { Categories = await this._dc.Categories .OrderBy(c => c.Name) .Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }) .ToListAsync() }; var query = this._dc.Questions .Include(x => x.Category) .Where(x => !x.DateAnswered.HasValue) .OrderByDescending(x => x.DateCreated); await model.GetData(query, pageNumber, this._cfg.PageSize); return(this.View(model)); }
public async Task <IActionResult> Questions(int pageNumber, QuestionsModel model) { // Validate posted data if (this.ModelState.IsValid) { // Create and save question entity var nq = new Question { QuestionText = model.Input.QuestionText, CategoryId = model.Input.CategoryId, DisplayName = model.Input.DisplayName, EmailAddress = model.Input.EmailAddress }; await this._dc.Questions.AddAsync(nq); await this._dc.SaveChangesAsync(); // Redirect to list of questions return(this.RedirectToAction( actionName: "Questions", controllerName: "Home", routeValues: new { pageNumber = string.Empty }, fragment: $"q_{nq.Id}")); } else { // Invalid data model.Categories = await this._dc.Categories .OrderBy(c => c.Name) .Select(c => new SelectListItem { Text = c.Name, Value = c.Id.ToString() }) .ToListAsync(); var query = this._dc.Questions .Include(x => x.Category) .Where(x => !x.DateAnswered.HasValue) .OrderByDescending(x => x.DateCreated); await model.GetData(query, pageNumber, this._cfg.PageSize); return(this.View(model)); } }