public ActionResult Create([Bind(Include = "Id,Name,Subscription")] Diet diet) { var ChoosenArr = Request.Cookies[namecookies]?.Value?.Split(','); int[] arr = Array.ConvertAll(ChoosenArr, s => int.Parse(s)); if (ModelState.IsValid && arr != null) { try { db.Diet.Add(diet); db.SaveChanges(); int Id_Add = (from i in db.Diet orderby i.Id descending select i).ToList()[0].Id; db.Diet.FirstOrDefault(i => i.Id == Id_Add).AddDishes(arr); db.SaveChanges(); } catch (Exception ex) { string e = ex.Message; ModelState.AddModelError("", "There is the same name "); return(View(diet)); } return(RedirectToAction("Index")); } ModelState.AddModelError("", "Error"); return(View(diet)); }
public ActionResult ChangeDiet(int id) { var name = HttpContext.User.Identity.Name; Users user = db.Users.FirstOrDefault(i => i.Email.CompareTo(name) == 0); db.UserDiet.RemoveRange(db.UserDiet.Where(i => i.User_Id == user.Id).ToList()); db.UserDiet.Add(new UserDiet() { Diet_Id = id, User_Id = user.Id, Date_Start = DateTime.Now }); db.SaveChanges(); return(RedirectToRoute(new { controller = "User", action = "Index", })); }
public static void UpdateDishAsync(this GoodFit bd, Dish newdish) { var dish = bd.Dish.FirstOrDefault(d => d.id == newdish.id); dish.Img = newdish.Img ?? dish.Img; dish.Name = newdish.Name; dish.Сalories = newdish.Сalories; dish.Contains_Meat = newdish.Contains_Meat; dish.Contains_Milk = newdish.Contains_Milk; dish.Contains_Sugar = newdish.Contains_Sugar; bd.SaveChanges(); }
// атрибуты Diet добавить при изминении update diet public static void UpdateDiet(this Diet newdiet, int[] arr) { using (var db = new GoodFit()) { Diet diet = db.Diet.FirstOrDefault(j => j.Id == newdiet.Id); diet.Name = newdiet.Name; diet.Subscription = newdiet.Subscription; diet.Description = newdiet.Description; if (arr.Length == 0) { db.Diet.FirstOrDefault(j => j.Id == newdiet.Id).Dish.Clear(); db.SaveChanges(); return; } foreach (var item in diet.Dish.ToList()) { if (arr.Contains(item.id)) { } else { db.Diet.FirstOrDefault(j => j.Id == newdiet.Id).Dish.Remove(db.Dish.FirstOrDefault(i => i.id == item.id)); } } db.SaveChanges(); diet.AddDishes(arr); db.SaveChanges(); } //db.Entry(diet).State = EntityState.Modified; //diet.Dish.Clear(); // db.Diet.FirstOrDefault(j => j.Id == diet.Id) }
public ActionResult AddDish(Dish dish, HttpPostedFileBase image1) { // if (ModelState.IsValid) { dish.Img = image1.ToByte(); db.Dish.Add(dish); try { db.SaveChanges(); } catch { db = new GoodFit(); } //} return(RedirectToAction("ShowDish")); }
public static void AddDishes(this Diet diet, int[] arr, bool save = true) { if (arr == null || arr.Length == 0) { throw new ArgumentException("Массив пустой"); } using (var db = new GoodFit()) { for (int i = 0; i < arr.Length; i++) { int Item_Id = arr[i]; Dish disht = db.Dish.FirstOrDefault(j => j.id == Item_Id) ?? throw new Exception(); db.Diet.FirstOrDefault(j => j.Id == diet.Id).Dish.Add(disht); //db.Dish.FirstOrDefault(j => j.id == Item_Id).Diet.Add(diet); } if (save) { db.SaveChanges(); } } }
public ActionResult Register(RegisterModel model, string Email) { if (ModelState.IsValid) { Users user = null; using (GoodFit db = new GoodFit()) { user = db.Users.FirstOrDefault(i => i.Name == model.Name); } if (user == null) { using (GoodFit db = new GoodFit()) { db.Users.Add(new Users() { Name = model.Name, Email = model.Email, Password = model.Password, Subscription = false }); try { db.SaveChanges(); } catch { ModelState.AddModelError("", "Error"); } user = db.Users.Where(i => i.Name == model.Name && i.Password == model.Password).FirstOrDefault(); } if (user != null) { FormsAuthentication.SetAuthCookie(model.Email, true); return(RedirectToAction("Index", "Home")); } } else { ModelState.AddModelError("", "Error"); } } return(View(model)); }