I have looked at Razor-pages form is not hitting the post method but it didn't help.
Waiver.cshtml
@page
@model GCRR.Models.WaiverLookup
@{
ViewData["Title"] = "Waiver lookup page";
}
<div id="page-wrapper">
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Sign a new or lookup an existing liability waiver
</h1>
</div>
</div>
<form method="post">
<div class="row">
<br />
<h3>
If you have signed a waiver that included the participates that are with you today, adults and/or kids, then enter your phone number or email address to lookup your waiver. You will need this when purchasing tickets.
If you have never signed a waiver before please click the new waiver button.
</h3>
</div>
<br />
<div class="row">
<div class="col-md-6">
<div class="row form-group">
<label asp-for="Phone" class="col-xs-12">Phone</label>
<div class="col-xs-9"><input asp-for="Phone" class="form-control" /></div>
<span asp-validation-for="Phone" class="text-danger"></span>
</div>
<br />or<br /><br />
<div class="row form-group">
<label asp-for="Email">Email</label>
<div><input asp-for="Email" class="form-control" /></div>
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="row form-group">
<input type="submit" class="btn btn-primary pull-right">
</div>
</div>
</div>
</form>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Waiver.cshtml.cs
using GCRR.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using GCRR.Models;
namespace GCRR.Pages
{
public class WaiverModel : PageModel
{
private IWaiverFileService _waiverFileService;
public WaiverModel(IWaiverFileService waiverFileService)
{
_waiverFileService = waiverFileService;
}
public void OnGet()
{
}
public void OnPost(WaiverLookup waiverLookup)
{
var waivers = _waiverFileService.SearchWaiversAsync("8137812796");
}
//public async Task<IActionResult> OnPostAsync(WaiverLookup waiverLookup)
//{
// if (!ModelState.IsValid)
// {
// return Page();
// }
// // Do Something
// var waivers = await _waiverFileService.SearchWaiversAsync("8137812796");
// return RedirectToPage("./Index");
//}
}
}
This is in Visual Studio 2022, I have a breakpoint set on the "OnPost" method but when I click the button the page refreshes and the breakpoint is not hit.
Has to be something dumb I'm missing here. Even if I put an OnGet method with a breakpoint that is not hit either when the page loads.
CodePudding user response:
Leave your WaiverLookup class in your Models folder.
In the WaiverModel class, add a property of type WaiverLookup
public class WaiverModel : PageModel
{
public WaiverLookup WaiverLookup { get; set;} // Your new property !!!
private IWaiverFileService _waiverFileService;
public WaiverModel(IWaiverFileService waiverFileService)
{
_waiverFileService = waiverFileService;
}
public void OnGet()
{
}
public void OnPost(WaiverLookup waiverLookup)
{
string phoneNumber = waiverLookup.Phone; // new value submitted
var waivers = _waiverFileService.SearchWaiversAsync("8137812796");
}
}
In your Waiver.cshtml page, you can now reference your new property WaiverLookup and access its members:
<input asp-for="WaiverLookup.Phone" class="form-control" />