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.