0

this is my method to Get Cart:

   [HttpGet("/UserPanel/CheckOut")]
   public async Task<IActionResult> CheckOut()
   {


       var text = await _siteService.GetSiteSettingForEdit();
       var tax = text.Tax;

       var userId = User.GetCurrentUserId();

       // Get From Session
       var cartItems = _orderService.GetFromSession();

       // Update ClientId In ShowCart after Login
       foreach (var item in cartItems)
       {
           if (item.ClientId == 0)
           {
               item.ClientId = userId;
           }
       }


       // Get user addresses
       var userAddresses = await _orderService.GetClientContactInfoById(userId);

       

       // we check it in repo and create a list:
       //    List<CartAddressViewModel> cartAddresses = new List<CartAddressViewModel>();
       //so we don't need to check if null, because in repo 100% create a list for it with object, but not null



       var deliveryAndPaymentDetails = GetDeliveryAndPaymentDetailsFromSession();
       //First get 0/0/1 then after run 2 script it will  be change to the new date

       // Create CartViewModel

       var cartViewModel = new CartViewModel
       {
           Items = cartItems,
           Tax = tax,
           CartAddresses = userAddresses,
           CartDeliveryDateViewModel = deliveryAndPaymentDetails

       };


       await GetStateAndDistricts();

       return View(cartViewModel);
   }

and I Add New Address in form 2 in my stepform :

      [HttpPost]
      public async Task<IActionResult> AddNewAddress(CartAddressViewModel model, int clientId)
      {
          await GetStateAndDistricts();
          var userId = User.GetCurrentUserId();

          var result = await _orderService.CreateNewContactInfoByClient(model, userId);

          switch (result)
          {
              case CreateCartAddressResult.NotFound:
                  return Json(new { success = false, message = "Address not found." });

              case CreateCartAddressResult.AddressIsExists:
                  return Json(new { success = false, message = "This Address is registered Before" });

              case CreateCartAddressResult.Success:

                  return Json(new { success = true, message = "Address added successfully!", formData = model });

              default:
                  return Json(new { success = false, message = "An unexpected error occurred." });
          }
      }

after adding I want to update View of Checkout but just form 2.
this is my Ajax after adding new Address, How ever I get the correct data in Checkout method with update data, But it dose not show in its view ( on step form 2)

  $("#addNewAddressContainer").on("submit", "form", function (e) {
  e.preventDefault();

  var $form = $(this);
  var formData = $form.serialize();

  $.ajax({
      url: $form.attr("action"),
      type: $form.attr("method"),
      data: formData,
      success: function (response) {
          if (response.success) {
              ShowMessage(response.message); // From My ShowMessage Method
              $form.hide();

              $('html, body').animate({ scrollTop: 0 }, 'fast');
              $('#addressListContainer').empty();

              // Update the address list after successful submission
              $.ajax({
                  url: "/UserPanel/CheckOut", // Endpoint to fetch updated checkout page HTML
                  type: "GET",
                  success: function (updatedView) {
                      console.log('Successfully fetched updated checkout page.');
                      console.log(updatedView); // Log the received HTML for debugging

                      $("#addressListContainer").html(updatedView);
                  
                  },
                     
                  error: function () {
                      alert('Error occurred while loading address list.');
                  }
              });
          } else {
              ShowMessage(response.message);
          }
      },
      error: function () {
          ShowMessage('Error occurred while submitting the form.');
      }
  });

});

what is my wrong to return new view after update? I check it in log I receive correct data and for example 4 address, but in view I see just 3 address, it means does not add new address that I add it by enter code here

2
  • did you log updatedAddresses? what is its value? Commented Jun 25 at 2:45
  • @LãNgọcHải I update my script in question. I check it in log I receive correct data and for example 4 address, but in view I see just 3 address, it means does not add new address that I add it script Commented Jun 25 at 13:51

0

Browse other questions tagged or ask your own question.