// GET: Admin/Products public async Task <IActionResult> Index(string searchString) { List <IndexProductViewModel> vms = new List <IndexProductViewModel>(); ViewData["CurrentFilter"] = searchString; var products = await _context.Products.ToListAsync(); if (!String.IsNullOrEmpty(searchString)) { products = products.Where(p => p.Name.Contains(searchString)).ToList(); } foreach (var item in products) { string mainImgStr = string.Empty; try { var mainImg = await _context.ProductImages.SingleAsync(i => i.ProductID == item.ProductID && i.IsMainImage); mainImgStr = mainImg.ImageUrl; } catch (Exception e) { _logger.LogError(e, "Fehler, beim ermittel eines Hauptatriklebildes!"); mainImgStr = "noImage.svg"; } if (string.IsNullOrWhiteSpace(mainImgStr)) { mainImgStr = "noImage.svg"; } IndexProductViewModel vm = new IndexProductViewModel() { ProductID = item.ProductID, ProductNumber = item.ProductNumber, Price = item.Price, AvailableQuantity = Math.Round(item.AvailableQuantity, 2), BasesUnit = new UnitHelper(_context, factory).GetUnitName(item.BasesUnitID), Description = item.Description, Name = item.Name, ShortDescription = item.ShortDescription, ShippingPriceTypeName = new ShippingPriceTypeHelper(_context).GetNameByID(item.ShippingPriceType), ShippingTime = new ShippingPeriodHelper(_context).GetDescription(item.ShippingPeriod), IsActive = item.IsActive, MainImage = mainImgStr }; vms.Add(vm); } //IQueryable<IndexProductViewModel> queryable = vms.AsQueryable(); //return View(await PaginatedList<IndexProductViewModel>.CeateAsync(queryable.AsNoTracking(), page ?? 1, pageSize)); return(View(vms)); }
// GET: Product public ActionResult Index() { var viewModel = new IndexProductViewModel(); viewModel.Categories = _db.Categories; viewModel.Products = _db.Products.Where(p => p.IsDeleted == false); viewModel.CategorySelectList = GetCategorySelectListItems(null); return(View(viewModel)); }
public ActionResult Index(int?category, int?group, Order sort = Order.not_sort, int page = 1) { IEnumerable <Product> products = _db.Products.Where(c => category == null || c.ProductGroup.ProductCategoryid == category). Where(g => group == null || g.ProductGroupId == group); PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = products.Count() }; switch (sort) { case Order.not_sort: products = products.OrderBy(C => C.Id).Skip((page - 1) * pageSize).Take(pageSize); break; case Order.first_expensive: products = products.OrderByDescending(C => C.Price).Skip((page - 1) * pageSize).Take(pageSize); break; case Order.first_cheap: products = products.OrderBy(C => C.Price).Skip((page - 1) * pageSize).Take(pageSize); break; } ViewBag.Name = "Vika"; IndexProductViewModel ivm = new IndexProductViewModel { PageInfo = pageInfo, Products = products, CurrentCategory = _db.ProductCategories.Where(c => c.Id == category).FirstOrDefault(), CurrentGroup = _db.ProductGroups.Where(g => g.Id == group).FirstOrDefault(), CurrentOrder = sort }; return(View(ivm)); }
public IEnumerable <IndexProductViewModel> GetProducts([FromRoute] int companyId) { List <Product> products = _context.Products.Where(x => x.Enabled && x.CompanyId == companyId).Include(x => x.Category).Include(x => x.SubCategory).Include(x => x.Brand).Include(x => x.Location).Include(x => x.ExchangeCurrency).ToList(); List <IndexProductViewModel> list = new List <IndexProductViewModel>(); string strRutaDefault = _config["ProductDefault"]; foreach (var item in products) { IndexProductViewModel model = new IndexProductViewModel { Id = item.Id, Awaiting = item.Awaiting, Brand = item.Brand?.Description, BrandId = item.BrandId, Category = item.Category?.Description, CategoryId = item.CategoryId, Codigo = item.Codigo, Cost = item.Cost, DateInitial = item.DateInitial.ToString("dd/MM/yyyy"), Description = item.Description, Discount = item.Discount, ExchangeCurrency = item.ExchangeCurrency?.Description, ExchangeCurrencyId = item.ExchangeCurrencyId, Gain = item.Gain, InStock = item.InStock, Location = item.Location?.Description, LocationId = item.LocationId, Name = item.Name, NameShort = item.NameShort, OutOfStock = item.OutOfStock, Price = item.Price, Stock = item.Stock, StockMin = item.StockMin, SubCategory = item.SubCategory?.Description, SubCategoryId = item.SubCategoryId, CheckStock = item.CheckStock }; if (!item.CheckStock.Value) { if (item.InStock.Value) { model.Status = "En Stock"; } if (item.OutOfStock.Value) { model.Status = "Sin Stock"; } if (item.Awaiting.Value) { model.Status = "En Espera"; } } else { if (item.Stock > item.StockMin) { model.Status = "En Stock"; } else { if (model.Stock <= item.StockMin && model.Stock > 0) { model.Status = "Ultimos"; } else { model.Status = "Sin Stock"; } } } if (!string.IsNullOrEmpty(item.Logo)) { byte[] imageArray = System.IO.File.ReadAllBytes(@item.Logo); string base64ImageRepresentation = Convert.ToBase64String(imageArray); model.Logo = "data:image/png;base64," + base64ImageRepresentation; } else { byte[] imageArray = System.IO.File.ReadAllBytes(strRutaDefault); string base64ImageRepresentation = Convert.ToBase64String(imageArray); model.Logo = "data:image/png;base64," + base64ImageRepresentation; } list.Add(model); } return(list.OrderBy(x => x.Name)); }