public void IndexActionShouldReturnTheModelIfPostIsNotValid() { var feedback = new FeedbackReport { Name = "Ivaylo", Content = "Test", }; var user = new UserProfile { UserName = LoggedUserName, Email = "*****@*****.**", }; this.EmptyOjsData.Users.Add(user); this.EmptyOjsData.SaveChanges(); var controller = new FeedbackController(EmptyOjsData); // assign the fake context var context = new ControllerContext(this.MockHttpContextBasePost(), new RouteData(), controller); controller.ControllerContext = context; var result = controller.Index(feedback, true) as ViewResult; var model = result.Model as FeedbackReport; Assert.AreEqual(model.Name, feedback.Name); Assert.AreEqual(model.Content, feedback.Content); }
public ActionResult Index(FeedbackViewModel model, bool captchaValid) { if (!captchaValid) { this.ModelState.AddModelError("Captcha", Resource.FeedbackIndex.Invalid_captcha); } if (this.ModelState.IsValid) { var report = new FeedbackReport { Content = model.Content, Email = model.Email, Name = model.Name }; if (this.User.Identity.IsAuthenticated) { var userProfile = this.Data.Users.GetByUsername(this.User.Identity.Name); report.User = userProfile; } this.Data.FeedbackReports.Add(report); this.Data.SaveChanges(); this.TempData[GlobalConstants.InfoMessage] = Resource.FeedbackIndex.Feedback_submitted; return this.RedirectToAction("Submitted"); } return this.View(model); }
public ActionResult Index(FeedbackReport feedback, bool captchaValid) { if (!captchaValid) { ModelState.AddModelError("Captcha", "Грешно въведен Captcha. Моля опитайте отново."); } if (ModelState.IsValid) { if (User.Identity.IsAuthenticated) { var userProfile = this.Data.Users.GetByUsername(User.Identity.Name); feedback.User = userProfile; } this.Data.FeedbackReports.Add(feedback); this.Data.SaveChanges(); return this.RedirectToAction("Submitted"); } return this.View(feedback); }