首页 > 解决方案 > 调用 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");
...
}

我希望有人能告诉我代码有什么问题吗?提前致谢

标签: c#htmlasp.net-corerazor.net-5

解决方案


推荐阅读