why is my handler not working, also the breakpoint doesn't even get hit for it
[ValidateAntiForgeryToken]
public IActionResult OnPostPlaceOrder()
{
try
{
if (!_userManager.UserIsGuest())
{
int? customerID = _customerManager.GetCustomerId();
if (customerID.HasValue)
{
var rentalOrder = new RentalOrder
{
CustomerID = customerID.Value,
RentalStartDate = DateTime.Now,
RentalEndDate = DateTime.Now.AddDays(7),
TotalCost = _rentalOrderManager.CalculateTotalCost(Cart),
Status = OrderStatus.Pending,
StartTime = TimeSpan.FromHours(8),
EndTime = TimeSpan.FromHours(18)
};
int orderId = _rentalOrderManager.PlaceOrder(Cart, rentalOrder);
return RedirectToPage("/OrderConfirmation", new { orderId });
}
}
return Page(); } catch (Exception ex) { _logger.LogError(ex, "An error occurred in OnPostPlaceOrder method"); return RedirectToPage("/Error"); } } public IActionResult OnPostRemoveFromCart(int ItemID) { _cartManager.RemoveItemFromCart(Cart, ItemID); return RedirectToPage("/Cart"); } } } ----------------------- <form method="post" asp-page-handler="PlaceOrder"> <button type="submit" class="btn btn-primary">Place Order</button> </form>
return Page(); } catch (Exception ex) { _logger.LogError(ex, "An error occurred in OnPostPlaceOrder method"); return RedirectToPage("/Error"); } } public IActionResult OnPostRemoveFromCart(int ItemID) { _cartManager.RemoveItemFromCart(Cart, ItemID); return RedirectToPage("/Cart"); } } } ----------------------- <form method="post" asp-page-handler="PlaceOrder"> <button type="submit" class="btn btn-primary">Place Order</button> </form>
4 Replies
Make sure in the designer the event is registered properly. If the event isn't registered it won't call the function. You can see that after selecting your button and checking the Properties pane
This is asp.net core, am i still able to see the properties? I think that’s only with windows forms
ah thought it was forms
looking at a tutorial, it says
"When the button is selected, a form POST request is sent to the server. By convention, the name of the handler method is selected based on the value of the handler parameter according to the scheme OnPost[handler]Async."
is it required to be async?
Did you check if you're currently going to that controller ? Like controller/OnPostPlaceOrder
And check if your breakpoint doest have a ⚠️ sign. It should be 🛑 only