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

Did you find this page helpful?