【发布时间】:2020-09-17 15:53:01
【问题描述】:
我正在尝试在我的 asp.net 核心网页中使用 IsPost 方法,但它说它在当前上下文中不存在。由于我没有使用 MVC(除了我有一个模型文件夹这一事实),我是否可以在我的剃须刀页面中使用 IsPost?基本上,我试图在用户点击提交按钮后在同一页面上显示确认文本,所以如果有人有更好的方法,请提出建议。谢谢
FreeConsultation.cshtml
@page
@model GuptaAccounting.Pages.FreeConsultationModel
@{
ViewData["Title"] = "FreeConsultation";
}
<head>
<script src="~/js/site.js"></script>
</head>
<div class="container" style="padding:30px;">
<br />
<h1 class="text-info">Get a FREE Consultation</h1>
<br />
@if (IsPost)
{
<p>Consultation requested. I will get back to you as soon as possible</p>
}
else
{
<form method="post">
<div class="text-danger" asp-validation-summary="ModelOnly"></div>
<!-- More of the form here -->
<div class="form-group row">
<div class="col-3 offset-3">
<input type="submit" value="Submit" onclick="return Validate()" class="btn btn-primary form-control" />
</div>
</div>
</form>
}
FreeConsultation.cshtml.cs
public class FreeConsultationModel : PageModel
{
private readonly ApplicationDbContext _db;
public FreeConsultationModel(ApplicationDbContext db)
{
_db = db;
}
[BindProperty]
public Client Client { get; set; }
public void OnGet()
{
}
public async Task<IActionResult> OnPost()
{
if (ModelState.IsValid)
{
Client.IsConsultationClient = true;
await _db.Client.AddAsync(Client);
await _db.SaveChangesAsync();
return RedirectToPage("Index");
}
else
{
return Page();
}
}
}
【问题讨论】:
标签: c# asp.net asp.net-core razor razor-pages