public async Task <ActionResult> Edit(CatVM model) { if (!ModelState.IsValid) { TempData["Errored"] = "Vui lòng kiểm tra lại các trường."; return(RedirectToRoute("AdminEditCat", new { id = model.cat_id })); } try { var _cat = await db.cats.FindAsync(model.cat_id); if (_cat != null) { _cat.cat_name = model.cat_name ?? null; _cat.cat_parent_id = model.cat_parent_id ?? null; _cat.cat_pos = model.cat_pos ?? null; _cat.cat_url = _cat.cat_name != null?configs.unicodeToNoMark(_cat.cat_name) : null; db.Entry(_cat).State = System.Data.Entity.EntityState.Modified; await db.SaveChangesAsync(); TempData["Updated"] = "Cập nhật danh mục thành công"; } return(RedirectToRoute("AdminListCat")); } catch (Exception ex) { TempData["Errored"] = "Có lỗi xảy ra khi cập nhật danh mục."; configs.SaveTolog(ex.ToString()); return(RedirectToRoute("AdminEditCat", new { id = model.cat_id })); } }
public async Task <ActionResult> Add(CatVM model) { if (!ModelState.IsValid) { TempData["Errored"] = "Vui lòng kiểm tra lại các trường."; return(RedirectToRoute("AdminAddCat")); } try { cat _new = new cat(); _new.cat_name = model.cat_name ?? null; _new.cat_parent_id = model.cat_parent_id ?? null; _new.cat_url = _new.cat_name != null?configs.unicodeToNoMark(_new.cat_name) : null; _new.cat_pos = model.cat_pos ?? null; db.cats.Add(_new); await db.SaveChangesAsync(); TempData["Updated"] = "Thêm danh mục thành công"; return(RedirectToRoute("AdminAddCat")); } catch (Exception ex) { ModelState.AddModelError("", "Có lỗi xảy ra khi thêm danh mục mới"); configs.SaveTolog(ex.ToString()); return(View(model)); } }
public async Task <ActionResult> Edit(int?id) { if (id == null || id == 0 || id == 1) { return(RedirectToRoute("Admin")); } cat _cat = await db.cats.FindAsync(id); if (_cat == null) { return(View(_cat)); } var getCat = new CatVM() { cat_id = _cat.cat_id, cat_name = _cat.cat_name, cat_parent_id = _cat.cat_parent_id, cat_pos = _cat.cat_pos }; ViewBag.TenCat = _cat.cat_name; return(View(getCat)); }
public List <StatVM> GetCat() { CatVM catVM = new CatVM(); decimal allQuan = _context.fridgeProducts.Sum(i => i.quantity); catVM.Fruit = _context.fridgeProducts.Where(i => i.Product.CategoryId == 1).Sum(i => i.quantity) / allQuan; catVM.Protein = _context.fridgeProducts.Where(i => i.Product.CategoryId == 2).Sum(i => i.quantity) / allQuan; catVM.Vegetable = _context.fridgeProducts.Where(i => i.Product.CategoryId == 3).Sum(i => i.quantity) / allQuan; List <StatVM> statVMs = new List <StatVM>(); statVMs.Add(new StatVM { type = "fruit", value = _context.fridgeProducts.Where(i => i.Product.CategoryId == 1).Sum(i => i.quantity) * 100 / allQuan }); statVMs.Add(new StatVM { type = "protein", value = _context.fridgeProducts.Where(i => i.Product.CategoryId == 2).Sum(i => i.quantity) * 100 / allQuan }); statVMs.Add(new StatVM { type = "vegetable", value = _context.fridgeProducts.Where(i => i.Product.CategoryId == 3).Sum(i => i.quantity) * 100 / allQuan }); return(statVMs); }