Reputation: 47
Hi I'm trying to make a Razor page with a simple form that add the new element into a list, but the OnPost()
method is never calling. I have a breakpoint on the first line of the method and it never hits, but in the OnGet()
it works.
This is my OnPost()
method:
public void OnPost()
{
newDocument = Int32.Parse(Request.Form[nameof(newDocument)]);
newAge = Int32.Parse(Request.Form[nameof(newAge)]);
newTriage = (Triage)Int32.Parse(Request.Form[nameof(newTriage)]);
newName = Request.Form[nameof(newName)];
newGender = Request.Form[nameof(newGender)];
newSymptoms = Request.Form[nameof(newSymptoms)];
var newPatient = new Patient
{
Document = newDocument,
Name = newName,
Age = newAge,
Gender = newGender,
Triage = newTriage,
Symptoms = newSymptoms
};
patients.Add(newPatient);
OrderPatients();
}
And the razor page have this form:
<form>
<div class="form-group">
<label for="patientDoc">Documento</label>
<input asp-for="newDocument" type="number" class="form-control" id="patientDoc" placeholder="ingrese documento">
</div>
<div class="form-group">
<label for="patientName">Nombre</label>
<input asp-for="newName" type="text" class="form-control" id="patientName" placeholder="Nombre">
</div>
<div class="form-group">
<label for="patientAge">Edad</label>
<input asp-for="newAge" type="number" class="form-control" id="patientAge" placeholder="Edad">
</div>
<div class="form-group">
<label for="patientGender">Género</label>
<input asp-for="newGender" type="text" class="form-control" id="patientGender" placeholder="Género">
</div>
<div class="form-group">
<label for="patientTri">Prioridad</label>
<select asp-for="newTriage" class="form-control" id="patientTri">
@foreach (Triage tri in Enum.GetValues(typeof(Triage)))
{
<option>@tri</option>
}
</select>
</div>
<div class="form-group">
<label for="patientSymp">Sintomas</label>
<input asp-for="newSymptoms" type="text" class="form-control" id="patientSymp" placeholder="Sintomas">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
I have read that you have to put this line @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
at the beginning of the page, but I did it and still the OnPost doesn't hit.
Anyone knows what is wrong?
Upvotes: 1
Views: 428
Reputation: 14655
The default method type for form
is GET
, so it hits your OnGet
handler because you didn't specify the method. Change <form>
to <form method="post">
and it should work.
Upvotes: 1
Reputation: 47
Forget it. I just had to add the method="form"
at the beginning of the form like this:
<form method="post">
. . .
</form>
Gonna leave this around here for another newbie like me who might need it.
Upvotes: 1