public JsonResult Create(FormCollection collection, int id, int questionID) { var ajaxResponse = new Dictionary<string, string>(); string body = collection["Answer"]; bool isCorrect = (collection["IsCorrect"] != "false"); var answer = new Answer { Body = body, IsCorrect = isCorrect, QuestionID = questionID }; try { CheckAnswerBody(body); CheckAnswer(answer); } catch (AnswerAjaxException ex) { ajaxResponse["success"] = "false"; ajaxResponse["message"] = ex.Message; return Json(ajaxResponse); } _db.Answers.AddObject(answer); _db.SaveChanges(); ajaxResponse["message"] = AnswerAjaxMessages.CREATE_COMPLETE; ajaxResponse["actionType"] = "create"; ajaxResponse["success"] = "true"; ajaxResponse["answerID"] = answer.ID.ToString(CultureInfo.InvariantCulture); ajaxResponse["body"] = body; ajaxResponse["isCorrect"] = (isCorrect) ? "true" : "false"; return Json(ajaxResponse); }
/// <summary> /// Create a new Answer object. /// </summary> /// <param name="id">Initial value of the ID property.</param> /// <param name="body">Initial value of the Body property.</param> /// <param name="isCorrect">Initial value of the IsCorrect property.</param> /// <param name="questionID">Initial value of the QuestionID property.</param> public static Answer CreateAnswer(global::System.Int32 id, global::System.String body, global::System.Boolean isCorrect, global::System.Int32 questionID) { Answer answer = new Answer(); answer.ID = id; answer.Body = body; answer.IsCorrect = isCorrect; answer.QuestionID = questionID; return answer; }
/// <summary> /// Deprecated Method for adding a new object to the Answers EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToAnswers(Answer answer) { base.AddObject("Answers", answer); }
/// <summary> /// Check answer is correct /// </summary> /// <param name="answer"></param> private void CheckAnswer(Answer answer) { if (answer == null) throw new AnswerAjaxException(AnswerAjaxMessages.NULL_ANSWER); }