public async Task <ActionResult> Edit(SurveyCategoriesViewModel viewModel) { if (!ModelState.IsValid) { /* Reload category list*/ viewModel.CategoryList = await _db.Category.ToListAsync(); return(View(viewModel)); } //_db.Entry(viewModel.Survey).State = EntityState.Modified; // Update survey var surveyFromDatabase = await _db.Survey.FindAsync(viewModel.Survey.Id); surveyFromDatabase.Name = viewModel.Survey.Name; surveyFromDatabase.Status = viewModel.Survey.Status; surveyFromDatabase.CategoryId = viewModel.Survey.CategoryId; // Update questions if (viewModel.Survey.Questions != null) { foreach (var question in viewModel.Survey.Questions) { _db.Entry(question).State = EntityState.Modified; } } await _db.SaveChangesAsync(); TempData["StatusMessage"] = "Survey updated successfully"; return(RedirectToAction(nameof(Index))); }
//[ActionName("Edit")] //public async Task<ActionResult> Edit(SurveyCategoryQuestionViewModel viewModel) //{ // return View(viewModel); //} //GET - EDIT public async Task <ActionResult> Edit(int?id) { if (id == null) { return(HttpNotFound()); } var survey = await _db.Survey.Include(s => s.Questions).FirstAsync(s => s.Id == id); if (survey == null) { return(HttpNotFound()); } var viewModel = new SurveyCategoriesViewModel { Survey = survey, CategoryList = await _db.Category.ToListAsync(), }; return(View(viewModel)); }