public async Task <IActionResult> AddOrEditQuestion(CreateSurveyViewModel createSurveyViewModel, Guid id, int?questionId) { if (questionId != null && ModelState.IsValid) { var survey = await _surveyService.EditQuestion(id, questionId ?? 0, createSurveyViewModel.QuestionText); return(RedirectToAction("Index", new { id = survey.Id })); } else if (ModelState.IsValid) { var survey = await _surveyService.AddQuestion(id, createSurveyViewModel.QuestionText); return(RedirectToAction("Index", new { id = survey.Id })); } else { //Get the survey var survey = await _surveyService.Get(id); //Decorate the ViewModel createSurveyViewModel.SurveyId = survey.Id; createSurveyViewModel.QuestionToEdit = questionId; createSurveyViewModel.QuestionText = questionId == null ? "" : survey.Questions[questionId ?? default(int)].Text; createSurveyViewModel.SurveyTitle = survey.Title; createSurveyViewModel.SurveyDescription = survey.Description; createSurveyViewModel.SurveyQuestions = survey.Questions; //Return the Index view return(View("Index", createSurveyViewModel)); } }