public ActionResult AddtoCart(int Id) { Cart cart = new Cart(); //var filterFood = db.Food.Where(f => f.FoodId == Id).SingleOrDefault(); var filterFood = customerRepo.CheckFood(Id); var userId = Convert.ToInt32(Session["UserId"]); //var filterCart = db.Carts.Where(c => c.UserId == userId && c.FoodId == Id).SingleOrDefault(); var filterCart = customerRepo.CheckCart(userId, Id); if (filterCart != null) { return(Json(new { message = "this food is already added", JsonRequestBehavior.AllowGet })); } else { cart.FoodId = Id; cart.FoodName = filterFood.FoodName; cart.TotalAmount = filterFood.FoodPrice; cart.FoodQuantity = 1; cart.UserId = userId; //db.Carts.Add(cart); //db.SaveChanges(); customerRepo.AddCart(cart); return(Json(new { message = "Food added to cartlist", JsonRequestBehavior.AllowGet })); } }