private void ValidateNewQuestiionInput(QuestionModel questionModel) { if (questionModel.TargetedMinAge != null && questionModel.TargetedMaxAge != null) { if (questionModel.TargetedMinAge > questionModel.TargetedMaxAge) { ModelState.AddModelError("TargetedMinAge", "Can not be lower than Max Age"); } } }
public ActionResult CreateQuestion(QuestionModel questionModel) { ValidateNewQuestiionInput(questionModel); this.LoadDropDownModelsInViewBag(); if (ModelState.IsValid) { var userName = this.User.Identity.Name; var currentUser = this.Data.Users.All().FirstOrDefault(u => u.UserName == userName); var newQuestion = new Question() { Text = questionModel.QuestionText, CategoryId = questionModel.CategoryId, Title = questionModel.QuestionTitle, TargetedMaxAge = questionModel.TargetedMaxAge, TargetedMinAge = questionModel.TargetedMinAge, Creator = currentUser, DateAsked = DateTime.Now }; if (questionModel.TargetedGender < 0) { newQuestion.TargetedGender = null; } else { newQuestion.TargetedGender = (Gender)questionModel.TargetedGender; } var user = this.Data.Users.All().FirstOrDefault(u => u.UserName == userName); newQuestion.Creator = user; user.Points -= PointsForQuestion; this.Data.Questions.Add(newQuestion); this.Data.SaveChanges(); this.ViewBag.ShowConfirm = true; this.ViewBag.PointsForQuestion = PointsForQuestion; return View("AskQuestion"); } JavaScriptSerializer js = new JavaScriptSerializer(); string json = js.Serialize(questionModel); ViewBag.JsonModel = json; return View("AskQuestion", questionModel); }
public ActionResult QuestionAnswers(int? id) { var question = this.Data.Questions.All().FirstOrDefault(q => q.QuestionId == id); // just in case of evil hacker var currUserId = this.User.Identity.GetUserId(); if (currUserId != question.Creator.Id) { return new HttpStatusCodeResult(HttpStatusCode.Unauthorized); } if (question != null) { var model = new QuestionModel() { QuestionId = question.QuestionId, QuestionText = question.Text, QuestionTitle = question.Title, CategoryId = question.CategoryId, TargetedGender = question.TargetedGender != null ? (int)question.TargetedGender : -1, TargetedGenderValue = question.TargetedGender != null ? question.TargetedGender.ToString() : null, TargetedMaxAge = question.TargetedMaxAge, TargetedMinAge = question.TargetedMinAge, }; if (question.TargetedGender != null) { model.TargetedGender = (int)question.TargetedGender; } return View(model); } return new HttpStatusCodeResult(HttpStatusCode.NotFound); }