我正在尝试在脚手架的Razor Pages表单上用今天的日期预填充日期表单字段。我尝试过在OnGet函数中设置类变量,也尝试过设置值(以下)甚至占位符。没有运气。
.Net Core 3.0 C#Web应用程序的剃须刀页面(不是mvc)。
以下两个日期选择器在我查看页面源时会在值中显示正确的日期,但是当页面加载时,我在选择器中得到的只是mm / dd / yyyy。我希望它在页面加载时向用户显示该日期。
<input asp-for="VideoAccess.AccessStartDate" value="@DateTime.Now" class="form-control" />
<input type="datetime-local" value="@DateTime.Now.ToString()" class="form-control" />
所有包装在Visual Studio中的代码生成支架。有什么想法吗?
答案 0 :(得分:0)
您只需在 OnGet()方法返回页面之前,将今天的日期分配给页面模型的datetime属性,然后再返回页面。
让我们通过使用Microsoft的Razor Pages with Entity Framework Core in ASP.NET Core Tutorial教师实体对此进行演示
中的相关部分。Pages / Instructors / Create.cshtml:
<div class="form-group">
<label asp-for="Instructor.HireDate" class="control-label"></label>
<input asp-for="Instructor.HireDate" class="form-control"/>
<span asp-validation-for="Instructor.HireDate" class="text-danger"></span>
</div>
Pages / Instructors / Create.cshtml.cs :(已删除的不相关部分)
[BindProperty]
public Instructor Instructor { get; set; }
public IActionResult OnGet()
{
Instructor ??= new Instructor{ HireDate = DateTime.Now };
return Page();
}
此行Instructor ??= new Instructor{ HireDate = DateTime.Now };
将当前日期时间值分配给绑定模型的属性教师。该页面将处理其余部分。