c# - 调用 OnPostPrint,第 1 次,第 3 次调用我得到默认日期 0001-01-01 00:00,第 2 次,第 4 次调用我得到真实日期,例如 2021-10-14 12:28
问题描述
嗨,我是 Razor 页面的新手,仅供参考,我使用的是 .net5。我的代码很简单:
[BindProperty]
public ReportDetail ReportQueryDetail { get; set; }
public class ReportDetail
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public async Task<IActionResult> OnGet(ReportDetail reportRequirement)
{
ReportQueryDetail = reportRequirement //this value is passed from a previous page to OnGet
..
}
public async Task<IActionResult> OnPostPrint()
{
await OnGet(ReportQueryDetail);
//get startDateTime and endDateTime
string startDateTime = ReportQueryDetail.StartDate.ToString("yyyy-MM-dd");
string endDateTime = ReportQueryDetail.EndDate.ToString("yyyy-MM-dd");
...
}
//在第 1、3 次调用 OnPostPrint 时,ReportQueryDetail 为空,因此开始日期和结束日期变为 0001-01-01 00:00,但在第 2、4 次调用时,startDateTime 和 endDateTime 上有值。我什么也没做,只是重复点击“打印”按钮
cshtml:
<form method="post">
<button type="submit" id="submit" name="submit" value="submit" class="btn-sm btn-outline-primary form-control" asp-page-handler="Print" asp-all-route-data="d">
Print
</button>
</form>
奇怪的是,我有另一个按钮叫保存,每次当我调用它 OnPostSave 时,我都能获取 ReportQueryDetail 的值
<div class="col-1">
<button type="submit" class="btn-sm btn-outline-primary form-control" asp-page-handler="SaveReport">Save</button>
</div>
public async Task<IActionResult> OnPostSave()
{
await OnGet(ReportQueryDetail);
//get startDateTime and endDateTime
string startDateTime = ReportQueryDetail.StartDate.ToString("yyyy-MM-dd");
string endDateTime = ReportQueryDetail.EndDate.ToString("yyyy-MM-dd");
...
}
我希望有人能告诉我代码有什么问题吗?提前致谢
解决方案
推荐阅读
- python - 如何使用 Pyrebase 使用 Firebase 实时数据库流?
- mysql - 1 表示单列中的 r 关系
- intellij-idea - 如何让 IntelliJ 在每个新项目中默认插入版权标头?
- python - 从 App Annie API 提取数据比较智能 - python
- excel - 使用excel VBA在IE中单击“另存为”
- ios - Travis CI 上的 iOS 排毒测试失败
- java - 矩阵乘法中的越界异常
- github - 为什么 Azure 管道突然停止出现在 GitHub 上的 PR(开源、公开)?
- linux - 接收 stdin 并调用以在 linux 中打开常规文件
- javascript - 无法在循环中设置未定义的属性“innerHTML”