public async Task <IActionResult> Products() { ShoppingCart cart; if (HttpContext.Session.GetString("varukorg") == null) { cart = new ShoppingCart { Products = new List <Matratt>() }; } else { var serializedValue = (HttpContext.Session.GetString("varukorg")); cart = JsonConvert.DeserializeObject <ShoppingCart>(serializedValue); } var allDishes = await _dishService.GetAllDishesAsync(); var model = new FoodMenu { PizzaDishes = allDishes.Where(d => d.MatrattTyp == 1), PastaDishes = allDishes.Where(d => d.MatrattTyp == 2), SaladDishes = allDishes.Where(d => d.MatrattTyp == 3), ShoppingCart = cart }; return(View(model)); }
public async Task <IActionResult> AddItem(int id) { var user = await _userManager.GetUserAsync(User); if (user == null) { Challenge(); } ShoppingCart cart; var product = await _dishService.GetOnlyDishAsync(id); // If shopping cart doesn't exist if (HttpContext.Session.GetString("varukorg") == null) { cart = new ShoppingCart { Products = new List <Matratt>(), User = user, Kund = await _userService.FindUserAsync(user.Id) }; } else { // Else get the session and cast it as cart var serializedValue = (HttpContext.Session.GetString("varukorg")); cart = JsonConvert.DeserializeObject <ShoppingCart>(serializedValue); } cart.Products.Add(product); //Lägga tillbaka listan i sessionsvariabeln var temp = JsonConvert.SerializeObject(cart); HttpContext.Session.SetString("varukorg", temp); return(PartialView("_CartList", cart)); }