public ActionResult CreateAnswer(Guid id) { var vm = new vm_AnswerEventQuestion(); vm.Questions = _eventQuestionRepo.FindAll(eq => eq.EventID == id).OrderBy(eq => eq.Question).ToList(); vm.EventQuestionIDs = _eventQuestionRepo.FindAll(eq => eq.EventID == id).OrderBy(e => e.Question).Select(e => e.ID.ToString()).Aggregate((i, j) => i + ";" + j); return View(vm); }
public ActionResult CreateAnswer(vm_AnswerEventQuestion Answer) { List<string> words = Answer.Words.Split(',').ToList(); foreach (var singleword in words) { var lowerword = singleword.ToLower().Trim(); var word = _wordRepository.FindAll(w => w.Word == lowerword).FirstOrDefault(); if (word != null) { word.WordCount = word.WordCount + 1; _wordRepository.Save(word); } else { var newword = new Words(); newword.WordCount = 1; newword.ID = Guid.NewGuid(); newword.Word = lowerword; _wordRepository.Save(newword); } } var eventQuestionIDs = Answer.EventQuestionIDs.Split(';').Select(e => new Guid(e)).ToList(); List<int> ar = new List<int>(); for(int n =0; n < Answer.StringAnswers.Length; n++) { ar.Add(int.Parse(Answer.StringAnswers[n].ToString())); } int i = 0; foreach (var id in eventQuestionIDs) { var qAnswer = new Answer { Age = Answer.Age, Username = Answer.Username, Email = Answer.Email, ID = Guid.NewGuid(), Gender = Answer.Gender, EventQuestionID = id, Score = ar[i] }; i++; try { _answerRepository.Save(qAnswer); } catch (DbEntityValidationException ex) { StringBuilder sb = new StringBuilder(); foreach (var failure in ex.EntityValidationErrors) { sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType()); foreach (var error in failure.ValidationErrors) { sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage); sb.AppendLine(); } } throw new DbEntityValidationException("Entity Validation Failed - errors follow:\n" + sb.ToString(), ex); } } if (!string.IsNullOrEmpty(Answer.Email)) { Subscriber subscriber = _subScriberRepository.FindAll(s => s.Email == Answer.Email).FirstOrDefault(); if(subscriber == null){ subscriber = new Subscriber(); subscriber.Email = Answer.Email; subscriber.City = Answer.City; subscriber.Name = Answer.Username; subscriber.ID = Guid.NewGuid(); _subScriberRepository.Save(subscriber); } } return RedirectToAction("CreateAnswer", "Event"); }