我有一个模型,它有一个日期,当我运行我的代码时,它给了我29/08/2019 00:00:00
。我希望它是29/08/2019
在我的模型中,我尝试了:
[DataType(DataType.Date)]
public DateTime? Date { get; set; }
和
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
public DateTime? Date { get; set; }
但两者似乎都不起作用,我一直在29/08/2019 00:00:00
.
我愿意接受使用剃须刀页面的建议,但我确实认为在模型中解决这个问题更容易。
你需要使用
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? Date { get; set; }
在Date
场上。
当我使用以下演示时,它运行良好:
型:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
[DisplayFormat(ApplyFormatInEditMode = false, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? Date { get; set; }
}
Index.cshtml.cs
public IList<Student> Student { get;set; }
public async Task OnGetAsync()
{
Student = await _context.Students.ToListAsync();
}
Index.cshtml
@foreach (var item in Model.Student) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@item.Date.Value.ToString("dd/MM/yyyy")
@*or below*@
@Html.DisplayFor(modelItem => item.Date)
</td>
</tr>
}