我看過Razor-pages 表單沒有點擊 post 方法,但它沒有幫助。
豁免.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");}
}
豁免.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");
//}
}
}
這是在 Visual Studio 2022 中,我在“OnPost”方法上設定了一個斷點,但是當我單擊該按鈕時,頁面會重繪 并且未命中該斷點。
一定是我在這里想念的愚蠢的東西。即使我放置了一個帶有斷點的 OnGet 方法,該斷點在頁面加載時也不會被擊中。
uj5u.com熱心網友回復:
將 WaiverLookup 類保留在 Models 檔案夾中。
在 WaiverModel 類中,添加型別為 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");
}
}
在您的 Waiver.cshtml 頁面中,您現在可以參考您的新屬性 WaiverLookup 并訪問其成員:
<input asp-for="WaiverLookup.Phone" class="form-control" />
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374203.html
