public ActionResult Create(Question question)
 {
     if (ModelState.IsValid)
       {
     questionRepository.InsertOrUpdate(question);
     questionRepository.Save();
     return RedirectToAction("SurveyQuestions", new { surveyId = question.SurveyId });
       }
       PopulateViewBag(question.SurveyId);
       return View(question);
 }
 public ActionResult Create(int surveyId)
 {
     var q = new Question()
         {
           Options = new List<QuestionOption>()
                       {
                         new QuestionOption(), new QuestionOption(), new QuestionOption()
                       },
           SurveyId = surveyId
         };
       PopulateViewBag(surveyId);
       return View(q);
 }
 public void InsertOrUpdate(Question question)
 {
     PurgeEmptyOptions(question);
       if (question.Id == default(int))
       {
     // New entity
     context.Questions.Add(question);
       }
       else
       {
     // Existing entity
     DeleteDestroyedOptions(question);
     SetQuestionId(question.Options, question.Id);
     context.SetEntityStateForChildren(question.Options);
     context.Entry(question).State = EntityState.Modified;
       }
 }
 private void DeleteDestroyedOptions(Question question)
 {
     context.ProcessDestroyableChildren(question.Options);
       var destroyed = question.Options.Where(x => x.Destroy && x.Id > 0).ToList();
       foreach (var questionOption in destroyed)
       {
     question.Options.Remove(questionOption);
       }
 }
 private static void PurgeEmptyOptions(Question question)
 {
     if(question.Options!=null)
       {
     var emptyOptions = question.Options.Where(option => string.IsNullOrWhiteSpace(option.OptionText)).ToList();
     foreach (var emptyOption in emptyOptions)
     {
       question.Options.Remove(emptyOption);
     }
       }
 }