ASP.Net 核心模型的日期 属性 在编辑模式下为空

ASP.Net core Model's date property is blank in edit mode

我有一个 Razor 页面网络应用程序,其中一个模型用于同事信息并包括他们的出生日期。

当我查看同事的脚手架页面时,出生日期字段填充在详细信息页面而不是编辑页面。

下面的图片将显示我的意思。

这是详细信息页面

这里是编辑页面

如您所知,由于编辑页面是空白的,如果我更改另一个字段,例如Staff 定位并保存,同事的DOB变为空

正如我所说,这些页面来自 EF Core 脚手架,所以我相信表单的 HTML 应该是正确的。

编辑页面HTML

<div class="form-group">
                <label asp-for="Colleague.DateOfBirth" class="control-label"></label>
                <input asp-for="Colleague.DateOfBirth" class="form-control" />
                <span asp-validation-for="Colleague.DateOfBirth" class="text-danger"></span>
            </div>

Colleague 是页面模型中 Colleague 模型的绑定 属性。如图所示,所有其他字段都可以正常填充。

更新

正如我所说,它正在使用模型绑定

[BindProperty]
public Colleague Colleague { get; set; }

OnPost


public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Attach(Colleague).State = EntityState.Modified;

            try
            {
                selectedBranch = Colleague.BranchID;
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ColleagueExists(Colleague.ColleagueID))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("../BranchColleagues", new { id = selectedBranch });
        }
[DataType(DataType.Date)]
        [Display(Name = "Date of Birth")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        public DateTime? DateOfBirth { get; set; }

我正在放置我的快速修复解决方案,因为这个问题已经空了一个月了。

通过从 POCO 中删除此注释 class:

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

日期值现在会根据需要显示在编辑表单中。

我也遇到了同样的问题。当我在输入标签

中使用 .Value 时它有效
<input asp-for="Colleague.DateOfBirth.Value" class="form-control" />