public JsonResult AddToCart(string id) { string username = Session.GetCurrentUserInfo("Username"); bool result = false; if (username == null) //not login => save in session { result = shoppingService.AddToCartSession(id); } else //login => save in DB { result = shoppingService.AddToCartDB(id, username); } if (result) //success { return(Json(new { success = true, responseText = "Add successfuly", quantity = -1 }, JsonRequestBehavior.AllowGet)); } else //fail { if (shoppingService.FailByQuantity) { //fail because quantity not enough => send available quantity in DB back to View to inform int quantityHave = shoppingService.GetItemQuantityInDB(id); return(Json(new { success = false, responseText = "Add fail because quantity", quantity = quantityHave }, JsonRequestBehavior.AllowGet)); } //fail return(Json(new { success = false, responseText = "Add fail", quantity = -1 }, JsonRequestBehavior.AllowGet)); } }