public void InsertOrUpdate(Volunteer volunteer) { if (volunteer.Id == default(int)) { // New entity context.Volunteers.Add(volunteer); } else { // Existing entity context.Entry(volunteer).State = EntityState.Modified; } }
public ActionResult Create(Volunteer volunteer) { if (ModelState.IsValid) { volunteerRepository.InsertOrUpdate(volunteer); volunteerRepository.Save(); return RedirectToAction("Index"); } else { ViewBag.PossibleGroups = groupRepository.All; return View(); } }
//[SpamProtection] public async Task<ActionResult> Join(Volunteer model) { model.RegistrationDate = DateTime.Now; string encodedResponse = Request.Form["g-Recaptcha-Response"]; bool isCaptchaValid = (ReCaptchaClass.Validate(encodedResponse) == "True"); if (!isCaptchaValid) { ModelState.AddModelError("captcha", "Ви не пройшли перевірку, спробуйте ще раз."); ViewBag.PossibleGroups = db.Groups.ToList(); return View(model); } if (ModelState.IsValid) { var count = db.Volunteers.Count(i => i.Email.Equals(model.Email, StringComparison.InvariantCultureIgnoreCase)); if (count > 0) { ViewBag.PossibleGroups = db.Groups.ToList(); ModelState.AddModelError("Email", ValidationResources.VolunteerExists); return View(model); } db.Volunteers.Add(model); db.SaveChanges(); await UserMailer.Welcome(model.Email).SendAsync(); return RedirectToAction("Thanks"); } ViewBag.PossibleGroups = db.Groups.ToList(); return View(model); }