C
C#8mo ago
G

✅ Razor pages, not reaching OnPost method

I have made a <form method="post"> and within it i have a <button type="submit" >. when i click on the button while debugging, the cshtml.cs uses the OnGet method. I have tried to assign a named handler to the button and adding the suffix the the onPost method and that does not work. I've looked up others same issues and mine looks fine but i must be messing something else up. Could I get a hand please, perhaps a look over?
63 Replies
JakenVeina
JakenVeina8mo ago
$details
MODiX
MODiX8mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
G
G8mo ago
My bad, not sure what all to include. Is it cool if i just upload the whole page? $code
MODiX
MODiX8mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
G
G8mo ago
using HotelManagementLibrary.Data.DataInterfaces; using HotelManagementLibrary.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; namespace HotelWebApp.Pages { public class BookingModel : PageModel { private IRead _read; private ICreate _create; [BindProperty(SupportsGet = true)] public DateTime StartDate { get; set; } [BindProperty(SupportsGet = true)] public DateTime EndDate { get; set; } [BindProperty(SupportsGet = true)] public int roomType1Choice { get; set; } [BindProperty(SupportsGet = true)] public int roomType2Choice { get; set; } [BindProperty] public List<RoomTypeModel> RoomOptions { get ; set ; } [BindProperty] public decimal StayTotal1 { get; set; } [BindProperty] public decimal StayTotal2 { get; set; } [BindProperty] public string FirstName { get; set; } [BindProperty] public string LastName { get; set; } public BookingModel(IRead read, ICreate create) { _read = read; _create = create; } public void OnGet() { StartDate = StartDate; EndDate = EndDate; roomType1Choice = roomType1Choice; roomType2Choice = roomType2Choice; RoomOptions = _read.GetRoomOptions(StartDate, EndDate); } public void OnPostReserve() { _create.Booking(FirstName, LastName, StartDate, EndDate, roomType1Choice, roomType2Choice); RedirectToPage("/Index"); } } }
G
G8mo ago
G
G8mo ago
towards the bottom, is the button that i can not get to post after i click on the confirm button it fires the OnGet method. I need it to fire the OnPost method. I've tried using with and without page handlers. IActionResult and void for the OnPost attribute, still nothing. @V.EINA Jaken , i hate to bother but can you look this over please
JakenVeina
JakenVeina8mo ago
uhm
I need it to fire the OnPost method
what OnPost method?
G
G8mo ago
Thanks for responding. public void OnPostReserve() at the bottom of the cshtml.cs code above.
JakenVeina
JakenVeina8mo ago
that is not OnPost
G
G8mo ago
I'm sorry, am I missing something
JakenVeina
JakenVeina8mo ago
OnPostReserve is not OnPost. It is OnPostReserve example documentation defines this method as....
public async Task<IActionResult> OnPostReserveAsync()
{

}
public async Task<IActionResult> OnPostReserveAsync()
{

}
G
G8mo ago
I wasn't doing any async, so i thought i wouldn't have to use
public async
public async
JakenVeina
JakenVeina8mo ago
well, async specifically, you may or may not need that's fair, that's not actually part of the example
G
G8mo ago
My button in the cshtml form is as follows
<button type="submit" class="btn btn-primary" asp-page-handler="reserve" >Confirm</button>
<button type="submit" class="btn btn-primary" asp-page-handler="reserve" >Confirm</button>
JakenVeina
JakenVeina8mo ago
indeed it is
G
G8mo ago
which should have fired the OnPostReserve method but does not. and i'm having trouble trying to identify why
JakenVeina
JakenVeina8mo ago
right that is indeed what we are here for so change your method to
public async Task<IActionResult> OnPostReserveAsync()
{

}
public async Task<IActionResult> OnPostReserveAsync()
{

}
G
G8mo ago
will do
JakenVeina
JakenVeina8mo ago
because that is the most obvious deviation between your code and code given by the documentation
G
G8mo ago
'''cs '''
public async Task<IActionResult>OnPostReserveAsync()
{
_create.Booking(FirstName,
LastName,
StartDate,
EndDate,
roomType1Choice,
roomType2Choice);

return RedirectToPage("/Index");
}
public async Task<IActionResult>OnPostReserveAsync()
{
_create.Booking(FirstName,
LastName,
StartDate,
EndDate,
roomType1Choice,
roomType2Choice);

return RedirectToPage("/Index");
}
this is what i changed to. The app hit OnGet first still
JakenVeina
JakenVeina8mo ago
let's see the rendered HTML for that button, then
G
G8mo ago
I'm sorry but I do not know what you are referring to when you say rendered HTML
JakenVeina
JakenVeina8mo ago
the HTML that your program is rendering and sending to the browser
G
G8mo ago
ohh ok
JakenVeina
JakenVeina8mo ago
and is then being run in the browser to make the button that you click that isn't doing what you want
G
G8mo ago
No description
JakenVeina
JakenVeina8mo ago
how about the form?
G
G8mo ago
No description
G
G8mo ago
is this what you were wanting to see?
JakenVeina
JakenVeina8mo ago
just the <form> tag, but yeah what's the actual request that's firing?
G
G8mo ago
on the browser dev tools?
JakenVeina
JakenVeina8mo ago
yes
G
G8mo ago
I'm not sure how to check that 😓
JakenVeina
JakenVeina8mo ago
"Network" tab
G
G8mo ago
I saw this in the console tab, Idk if it helps
G
G8mo ago
No description
G
G8mo ago
I'm not seeing anything saying "request"
MODiX
MODiX8mo ago
JakenVeina
"Network" tab
Quoted by
React with ❌ to remove this embed.
G
G8mo ago
i'm on the network tab, i'm trying to look somewhere that says request or similar so can tell you what's firing
JakenVeina
JakenVeina8mo ago
everything in the tab is a request it is a list of requests
G
G8mo ago
No description
JakenVeina
JakenVeina8mo ago
click the button, and a new request will be added look at that
G
G8mo ago
the method says GET, idk why
G
G8mo ago
No description
JakenVeina
JakenVeina8mo ago
k alright, current theory:
<button type="submit" class="btn btn-primary" asp-page-handler="reserve" >Confirm</button>
<button type="submit" class="btn btn-primary" asp-page-handler="reserve" >Confirm</button>
this is not the button you're clicking on
G
G8mo ago
when i select the button in dev tools it highlights the correct button
No description
JakenVeina
JakenVeina8mo ago
oh wait you have invalid HTML
G
G8mo ago
could it be the formaction
G
G8mo ago
No description
JakenVeina
JakenVeina8mo ago
no you have invalid HTML
<form method="post
<div class=" text-center">
<form method="post
<div class=" text-center">
G
G8mo ago
you're saying that's what i need to change mine to?
JakenVeina
JakenVeina8mo ago
no that's what you have
G
G8mo ago
😃
JakenVeina
JakenVeina8mo ago
now you can probably put your handler method back how it was
G
G8mo ago
thank you SO much. how embarrassing! lol
JakenVeina
JakenVeina8mo ago
not really the browser really OUGHT to have rejected that
G
G8mo ago
you're right, it's just i've spent hours looking and reading. and bothering you for a simple oversight
JakenVeina
JakenVeina8mo ago
or your editor always work the problem don't work around the problem
G
G8mo ago
yeah, i would think some squigly lines would have popped up somewhere
JakenVeina
JakenVeina8mo ago
if the problem is that a button isn't doing what you want, start by looking at the button and, by extension, the <form> that it's in
G
G8mo ago
great advice! I kept thinking it was the C# code. Thank you again. I learned a lot from this so i'm super greatful
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.