C
C#14mo 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
JakenVeina14mo ago
$details
MODiX
MODiX14mo 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
GOP14mo ago
My bad, not sure what all to include. Is it cool if i just upload the whole page? $code
MODiX
MODiX14mo 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
GOP14mo 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
GOP14mo ago
G
GOP14mo 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
JakenVeina14mo ago
uhm
I need it to fire the OnPost method
what OnPost method?
G
GOP14mo ago
Thanks for responding. public void OnPostReserve() at the bottom of the cshtml.cs code above.
JakenVeina
JakenVeina14mo ago
that is not OnPost
G
GOP14mo ago
I'm sorry, am I missing something
JakenVeina
JakenVeina14mo 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
GOP14mo ago
I wasn't doing any async, so i thought i wouldn't have to use
public async
public async
JakenVeina
JakenVeina14mo ago
well, async specifically, you may or may not need that's fair, that's not actually part of the example
G
GOP14mo 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
JakenVeina14mo ago
indeed it is
G
GOP14mo ago
which should have fired the OnPostReserve method but does not. and i'm having trouble trying to identify why
JakenVeina
JakenVeina14mo 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
GOP14mo ago
will do
JakenVeina
JakenVeina14mo ago
because that is the most obvious deviation between your code and code given by the documentation
G
GOP14mo 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
JakenVeina14mo ago
let's see the rendered HTML for that button, then
G
GOP14mo ago
I'm sorry but I do not know what you are referring to when you say rendered HTML
JakenVeina
JakenVeina14mo ago
the HTML that your program is rendering and sending to the browser
G
GOP14mo ago
ohh ok
JakenVeina
JakenVeina14mo ago
and is then being run in the browser to make the button that you click that isn't doing what you want
G
GOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
how about the form?
G
GOP14mo ago
No description
G
GOP14mo ago
is this what you were wanting to see?
JakenVeina
JakenVeina14mo ago
just the <form> tag, but yeah what's the actual request that's firing?
G
GOP14mo ago
on the browser dev tools?
JakenVeina
JakenVeina14mo ago
yes
G
GOP14mo ago
I'm not sure how to check that 😓
JakenVeina
JakenVeina14mo ago
"Network" tab
G
GOP14mo ago
I saw this in the console tab, Idk if it helps
G
GOP14mo ago
No description
G
GOP14mo ago
I'm not seeing anything saying "request"
MODiX
MODiX14mo ago
JakenVeina
"Network" tab
Quoted by
React with ❌ to remove this embed.
G
GOP14mo 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
JakenVeina14mo ago
everything in the tab is a request it is a list of requests
G
GOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
click the button, and a new request will be added look at that
G
GOP14mo ago
the method says GET, idk why
G
GOP14mo ago
No description
JakenVeina
JakenVeina14mo 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
GOP14mo ago
when i select the button in dev tools it highlights the correct button
No description
JakenVeina
JakenVeina14mo ago
oh wait you have invalid HTML
G
GOP14mo ago
could it be the formaction
G
GOP14mo ago
No description
JakenVeina
JakenVeina14mo ago
no you have invalid HTML
<form method="post
<div class=" text-center">
<form method="post
<div class=" text-center">
G
GOP14mo ago
you're saying that's what i need to change mine to?
JakenVeina
JakenVeina14mo ago
no that's what you have
G
GOP14mo ago
😃
JakenVeina
JakenVeina14mo ago
now you can probably put your handler method back how it was
G
GOP14mo ago
thank you SO much. how embarrassing! lol
JakenVeina
JakenVeina14mo ago
not really the browser really OUGHT to have rejected that
G
GOP14mo ago
you're right, it's just i've spent hours looking and reading. and bothering you for a simple oversight
JakenVeina
JakenVeina14mo ago
or your editor always work the problem don't work around the problem
G
GOP14mo ago
yeah, i would think some squigly lines would have popped up somewhere
JakenVeina
JakenVeina14mo 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
GOP14mo 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
Accord13mo 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.
Want results from more Discord servers?
Add your server