public ActionResult Create(StatCreate model) { if (!ModelState.IsValid) { return(View(model)); } var service = CreateStatService(); if (service.CreateStat(model)) { TempData["SaveResult"] = "Stats added."; return(RedirectToAction("Index")); } ; ModelState.AddModelError("", "Stats could not be created."); return(View(model)); }
public bool CreateStat(StatCreate model) { var context = new ApplicationDbContext(); var user = context.KJPUser.Single(e => e.OwnerId == _userId); var entity = new Stat() { UserStatId = user.UserId, Weight = model.Weight, WeightDate = DateTimeOffset.Now, GoalMessage = model.GoalMessage }; using (var ctx = new ApplicationDbContext()) { ctx.Stats.Add(entity); return(ctx.SaveChanges() == 1); } }