public async Task <IActionResult> Create(IFormCollection form) { if (ModelState.IsValid) { Category category = new Category(); category.Name = form["Category.Name"]; category.Description = form["Category.Description"]; _context.Add(category); await _context.SaveChangesAsync(); int id = category.Id; int cnt = Convert.ToInt32(form["CatSpecsCtr"]); for (int i = 0; i <= cnt; i++) { var catSpecName = form["CategorySpecs[" + i + "].Name"]; if (catSpecName.Count > 0) { CategorySpec tmp = new CategorySpec(); tmp.Name = catSpecName; tmp.CategoryId = id; _context.Add(tmp); } } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View()); }
public async Task <CategoryViewModel> GetCategoryItems(int pageIndex, int itemsPage, int?typeId) { var filterSpecification = new CategorySpec(typeId); var filterPaginatedSpecification = new CategoryFilterSpec(itemsPage * pageIndex, itemsPage, typeId); var itemsOnPage = _prodRepository.ListAll();//.List(filterPaginatedSpecification).ToList(); var totalItems = _prodRepository.Count(filterSpecification); var vm = new CategoryViewModel() { CategoryItems = itemsOnPage.Select(i => new CategoryItemViewModel() { Id = i.Id, Name = i.Name, Price = i.Price }), Types = await GetTypes(), TypesFilterApplied = typeId ?? 0, PaginationInfo = new PaginationVM() { ActualPage = pageIndex, ItemsPerPage = itemsOnPage.ToList().Count(), TotalItems = totalItems, TotalPages = int.Parse(Math.Ceiling(((decimal)totalItems / itemsPage)).ToString()) } }; vm.PaginationInfo.Next = (vm.PaginationInfo.ActualPage == vm.PaginationInfo.TotalPages - 1) ? "is-disabled" : ""; vm.PaginationInfo.Previous = (vm.PaginationInfo.ActualPage == 0) ? "is-disabled" : ""; return(vm); }
// GET: Products/Details/5 public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var product = await _context.Products .Include(p => p.Category) .Include(p => p.Factory) .FirstOrDefaultAsync(m => m.Id == id); var productSpecs = _context.ProductSpecs.Where(ps => ps.ProductId == product.Id).ToList(); List <CategorySpec> categorySpecs = new List <CategorySpec>(); foreach (var productSpec in productSpecs) { CategorySpec categorySpec = _context.CategorySpecs.Where(cs => cs.Id == productSpec.CategorySpecId).FirstOrDefault(); categorySpecs.Add(categorySpec); } var productUniqueSpecs = _context.ProductUniqueSpecs.Where(pus => pus.ProductId == product.Id).ToList(); var Model = new ProductDetailsViewModel(); Model.Product = product; Model.ProductUniqueSpecs = productUniqueSpecs; Model.ProductSpecs = productSpecs; Model.CategorySpecs = categorySpecs; if (product == null) { return(NotFound()); } return(View(Model)); }
public async Task <IActionResult> Edit(IFormCollection form) { int id = Convert.ToInt32(form["Category.Id"]); var category = _context.Categories.Where(c => c.Id == id).FirstOrDefault(); if (category == null) { return(NotFound()); } if (ModelState.IsValid) { try { category.Id = id; category.Name = form["Category.Name"]; category.Description = form["Category.Description"]; var oldSpecs = _context.CategorySpecs.Where(cs => cs.CategoryId == id).ToList(); var oldSpecsCtr = oldSpecs.Count; for (int i = 0; i < oldSpecsCtr; i++) { var tmpSpecId = form["OldCategorySpecs[" + i + "].Id"]; if (tmpSpecId.Count > 0) { int specId = Convert.ToInt32(form["OldCategorySpecs[" + i + "].Id"]); if (specId == oldSpecs[i].Id) { oldSpecs[i].Name = form["OldCategorySpecs[" + i + "].Name"]; _context.Update(oldSpecs[i]); } else { var productSpecs = _context.ProductSpecs.Where(ps => ps.CategorySpecId == oldSpecs[i].Id).ToList(); foreach (var productSpec in productSpecs) { _context.Remove(productSpec); } _context.Remove(oldSpecs[i]); } } else { var productSpecs = _context.ProductSpecs.Where(ps => ps.CategorySpecId == oldSpecs[i].Id).ToList(); foreach (var productSpec in productSpecs) { _context.Remove(productSpec); } _context.Remove(oldSpecs[i]); } } int cnt = Convert.ToInt32(form["CatSpecsCtr"]); for (int i = 0; i <= cnt; i++) { var catSpecName = form["CategorySpecs[" + i + "].Name"]; if (catSpecName.Count > 0) { CategorySpec tmp = new CategorySpec(); tmp.Name = catSpecName; tmp.CategoryId = id; _context.Add(tmp); } } _context.Update(category); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(category.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View()); }