errors in Cart Count update, items not adding Cart, and Cart Total is not updating

Thibaut Fabrice 20 Reputation points
2024-10-27T11:09:34.3566667+00:00

Thank you in advance,

Can you help me remove the errors?

My github Link:Thibaut501/Mango. errors are Count of items cannot be updated, Items is not adding to Cart and Order Total is not updating on Applying Coupon .Discount is not appearing also.

its from a course is on Udemy:.NET Core Microservices - The Complete Guide (.NET 8 MVC) from Bruygen Patel.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
14,030 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,039 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hongrui Yu-MSFT 2,785 Reputation points Microsoft Vendor
    2024-10-28T09:59:29.35+00:00

    Hi,@Thibaut Fabrice. Welcome to Microsoft Q&A. 

    Failed to add product to shopping cart.

    Reason:

    1.In your Mango.Web->Models->CartHeaderDto, Name, Phone, and Email are added with [Required], but no value is passed when sending a network request.

    2.[FromBody] is missing in Mango.Services.ShoppingCartAPI->Controllers->CartAPIController->CartUpsert method.

    Final rendering effect: The network request is not sent to CartUpsert for execution.

    Reference code: For CartHeaderDto, I used String.Empty to assign a default value.

    public class CartHeaderDto
    {
        
        public int CartHeaderId { get; set; }
        public string? UserId { get; set; } 
        public string? CouponCode { get; set; }      
        public double Discount { get; set; }       
        public double CartTotal { get; set; }
    
        [Required]
        public string? Name { get; set; } = String.Empty;
        [Required]
        public string? Phone { get; set; } = String.Empty;
        [Required]
        public string? Email { get; set; } = String.Empty;
    }
    

    For CartUpsert, I added [FromBody].

    public async Task<ResponseDto> CartUpsert([FromBody] CartDto cartDto)
    

    Unable to use coupons.

    The Model is not passed correctly in Mango.Web->Views->Cart->CartIndex.cshtml. The asp-route-cartDto="@Model" of the Apply button and the Remove button needs to be deleted. In addition, the asp-action="Remove Coupon" of the Remove button has extra spaces, which need to be deleted.

    Modified code

    <div class="col-6">
        @if (string.IsNullOrEmpty(Model.CartHeader.CouponCode))
        {
            <span>Coupon:</span>
            <input asp-for="CartHeader.CouponCode" />
            <button type="submit" asp-action="ApplyCoupon"  class="btn btn-sm btn-success">Apply</button>
        }
        else
        {
            <span>Coupon:</span>
            <input asp-for="CartHeader.CouponCode" disabled />
            <button type="submit" asp-action="RemoveCoupon"  class="btn btn-sm btn-success">Remove</button>
        }
    </div>
    

    In addition, in the ApplyCoupon and RemoveCoupon methods in Mango.Services.ShoppingCartAPI->Controllers->CartAPIController, you only assigned cartFromDb.CouponCode but not cartFromDb.Discount, and CartIndex.cshtml needs to use the following code to display the discounted information.

    @if (Model.CartHeader.Discount > 0)
    {
        <span class="text-success">Order Discount: @string.Format("{0:c}", Model.CartHeader.Discount)</span>
    }
    

    Therefore, you need to obtain the complete information of the coupon and assign it to cartFromDb.Discount (depending on your needs, see if you need to calculate the discounted total price additionally).


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Thibaut Fabrice 20 Reputation points
    2024-11-17T07:41:36.56+00:00

    https://github.com/Thibaut501/Mango

    Hi,

    Thanks for the help. However, My stripe is not loading. Any help. There is an error: Object not set to an instance of an object.

    Awaiting your reply,

    Best Regards,

    FabriceScreenshot (460)

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.