public async Task <ActionResult> Create(Treat treat, int FlavorId) { var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var currentUser = await _userManager.FindByIdAsync(userId); treat.User = currentUser; if (treat.Name == null) { TempData["message"] = "Name required! Please enter name to continue."; return(Create()); } else if (_db.Treats.FirstOrDefault(t => t.Name.ToLower() == treat.Name.ToLower()) != null) { TempData["message"] = "Treat already exists!"; return(Create()); } else { _db.Treats.Add(treat); if (FlavorId != 0) { _db.FlavorTreat.Add(new FlavorTreat() { FlavorId = FlavorId, TreatId = treat.TreatId }); } _db.SaveChanges(); return(RedirectToAction("Index")); } }
public async Task <ActionResult> Create(Flavor flavor) { var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var currentUser = await _userManager.FindByIdAsync(userId); flavor.User = currentUser; if (flavor.Title == null) { TempData["message"] = "Title required! Please enter title to continue."; return(Create()); } else if (_db.Flavors.FirstOrDefault(f => f.Title.ToLower() == flavor.Title.ToLower()) != null) { TempData["message"] = "Flavor already exists!"; return(Create()); } else { _db.Flavors.Add(flavor); _db.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult Create(Flavor flavor, int treatId) { _db.Flavors.Add(flavor); _db.SaveChanges(); if (treatId != 0) { _db.FlavorTreats.Add(new FlavorTreat() { FlavorId = flavor.FlavorId, TreatId = treatId }); } _db.SaveChanges(); return(RedirectToAction("Index")); }