✅ 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
$details
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)
My bad, not sure what all to include. Is it cool if i just upload the whole page?
$code
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/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");
}
}
}
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
uhm
I need it to fire the OnPost methodwhat OnPost method?
Thanks for responding. public void OnPostReserve() at the bottom of the cshtml.cs code above.
that is not
OnPost
I'm sorry, am I missing something
OnPostReserve
is not OnPost
. It is OnPostReserve
example documentation defines this method as....
I wasn't doing any async, so i thought i wouldn't have to use
well,
async
specifically, you may or may not need
that's fair, that's not actually part of the exampleMy button in the cshtml form is as follows
indeed it is
which should have fired the OnPostReserve method but does not. and i'm having trouble trying to identify why
right
that is indeed what we are here for
so change your method to
will do
because that is the most obvious deviation between your code and code given by the documentation
'''cs '''
this is what i changed to. The app hit OnGet first still
let's see the rendered HTML for that button, then
I'm sorry but I do not know what you are referring to when you say rendered HTML
the HTML that your program is rendering
and sending to the browser
ohh ok
and is then being run in the browser
to make the button
that you click
that isn't doing what you want
how about the form?
is this what you were wanting to see?
just the
<form>
tag, but yeah
what's the actual request that's firing?on the browser dev tools?
yes
I'm not sure how to check that 😓
"Network" tab
I saw this in the console tab, Idk if it helps
I'm not seeing anything saying "request"
JakenVeina
"Network" tab
Quoted by
<@137791696325836800> from #Razor pages, not reaching OnPost method (click here)
React with ❌ to remove this embed.
i'm on the network tab, i'm trying to look somewhere that says request or similar so can tell you what's firing
everything in the tab is a request
it is a list of requests
click the button, and a new request will be added
look at that
the method says GET, idk why
k
alright, current theory:
this is not the button you're clicking on
when i select the button in dev tools it highlights the correct button
oh
wait
you have invalid HTML
could it be the formaction
no
you have invalid HTML
you're saying that's what i need to change mine to?
no
that's what you have
😃
now you can probably put your handler method back how it was
thank you SO much. how embarrassing! lol
not really
the browser really OUGHT to have rejected that
you're right, it's just i've spent hours looking and reading. and bothering you for a simple oversight
or your editor
always work the problem
don't work around the problem
yeah, i would think some squigly lines would have popped up somewhere
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 ingreat advice! I kept thinking it was the C# code. Thank you again. I learned a lot from this so i'm super greatful
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.