Exemplo n.º 1
0
        public async Task <IActionResult> Index(string sortOrder)
        {
            try
            {
                var cart = SessionHelper.GetObjectFromJson <List <Product> >(HttpContext.Session, CartController.getKey());
                if (cart == null)
                {
                    var productsCheck = _products.GetProducts().FindAll(p => p.State == State.InCart);
                    productsCheck.ForEach(p => p.State = State.InStore);
                    _context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date";
            var products = from s in _context.Products
                           select s;

            switch (sortOrder)
            {
            case "name_desc":
                products = products.OrderByDescending(s => s.Title);
                break;

            case "Date":
                products = products.OrderBy(s => s.Date);
                break;

            case "date_desc":
                products = products.OrderByDescending(s => s.Date);
                break;

            default:
                products = products.OrderBy(s => s.Title);
                break;
            }
            return(View(await products.AsNoTracking().ToListAsync()));
        }
Exemplo n.º 2
0
 public override void OnActionExecuted(ActionExecutedContext context)
 {
     try
     {
         base.OnActionExecuted(context);
         try
         {
             var _activeuser = JsonConvert.DeserializeObject <User>(_httpContext.HttpContext.Request.Cookies["login_user"]);
             ViewBag.activeUser = _activeuser;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.Message);
         }
         try
         {
             var cart = SessionHelper.GetObjectFromJson <List <Product> >(HttpContext.Session, CartController.getKey());
             ViewBag.Cart = cart;
         }
         catch
         {
             Console.WriteLine("error");
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }