public async Task <IActionResult> Edit(int id, [Bind("ID,Question,Author,QuestionAuthor,QuestionDate")] QOTDAnswer qOTDAnswer) { if (id != qOTDAnswer.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(qOTDAnswer); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!QOTDAnswerExists(qOTDAnswer.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(qOTDAnswer)); }
public async Task <IActionResult> Create([Bind("ID,Question,QuestionID,Authors,QuestionDate,Answer")] QOTDAnswerCreateEditViewModel QOTDAnswerVM, IFormCollection form) { Debug.Print(QOTDAnswerVM.QuestionID.ToString()); //not sure I want to allow multiple authors // need to swich to creating a different row for each author instead of saving multiple author ids string AuthorIds = ""; foreach (string item in form["Authors"]) { string ite = item; AuthorIds += ite + ",,"; } QOTDAnswer qOTDAnswer = new QOTDAnswer(); qOTDAnswer.Question = QOTDAnswerVM.Question; qOTDAnswer.QuestionID = QOTDAnswerVM.QuestionID; qOTDAnswer.Answer = QOTDAnswerVM.Answer; qOTDAnswer.QuestionDate = QOTDAnswerVM.QuestionDate; qOTDAnswer.Author = AuthorIds; Debug.Print(qOTDAnswer.Author); if (ModelState.IsValid) { _context.Add(qOTDAnswer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(qOTDAnswer)); }
public async Task <IActionResult> AddQuestionAndAnswer(QOTDAnswer newAnswer, IFormCollection form) { if (!ModelState.IsValid) { return(RedirectToAction("About")); } Debug.Write(newAnswer.Author); Debug.Write(newAnswer.AuthorName); int intIdt = _context.QOTDQuestion.Max(u => u.id); newAnswer.QuestionID = intIdt; List <string> AuthorIds = new List <string> { }; foreach (string item in form["Authors"]) { Debug.Write("##############################################################################################"); Debug.WriteLine(item); AuthorIds.Add(item); Debug.WriteLine(AuthorIds[0]); } Debug.Write("##############################################################################################"); Debug.WriteLine(AuthorIds[0]); Debug.WriteLine(AuthorIds.Count()); var successful = false; if (AuthorIds.Count() <= 1) { newAnswer.Author = AuthorIds[0]; successful = await _todoItemService.AddItemAsync(newAnswer); successful = await _todoItemService.AddQuestionAsync(newAnswer.Question); } else { List <QOTDAnswer> allAuthors = new List <QOTDAnswer> () { }; foreach (string author in AuthorIds) { newAnswer.Author = author; allAuthors.Add(newAnswer); successful = await _todoItemService.AddItemsAsync(allAuthors); } } Debug.Write(successful); if (!successful) { return(BadRequest("Could not add item.")); } return(RedirectToAction("About", new{ QuestionID = intIdt })); }
public async Task <bool> AddItemAsync(QOTDAnswer newItem) { newItem.insertionDate = DateTime.Now; _context.QOTDAnswers.Add(newItem); var saveResult = await _context.SaveChangesAsync(); return(saveResult == 1); }