[英]Specify a format for “asp-for” HTML Tag (ASP.NET Core)
In an ASP.NET Core project I have to display a (readonly) date in a specific format (say "dd/mm/yyyy HH:MM") 在ASP.NET Core项目中,我必须以特定格式显示(只读)日期(例如“dd / mm / yyyy HH:MM”)
<div class="form-group">
<label asp-for="Date" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="Date" readonly class="form-control" />
</div>
</div>
How can I do it, knowing that the Date
field is declared in the model like this 我怎么能这样做,因为知道
Date
字段是在模型中声明的
public DateTime Date { get { return this.Timestamp.DateTime; } }
? ?
Alternatively, you could decorate the property with DisplayFormatAttribute
. 或者,您可以使用
DisplayFormatAttribute
修饰属性。
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy HH:MM}")]
public DateTime Date { get { return this.Timestamp.DateTime; } }
The markup you have would not need to change. 您拥有的标记无需更改。
If it is read only, you could hardcode as - 如果它是只读的,您可以硬编码为 -
<input value="@Model.Date.ToString("dd/MM/yyyy HH:mm")" readonly class="form-control" />
Or display as text 或显示为文本
<div class="form-group">
<label asp-for="Date" class="col-md-2 control-label"></label>
<div class="col-md-10 form-control-static">
@Model.Date.ToString("dd/MM/yyyy HH:mm")
</div>
</div>
尝试这个。
<input asp-for="Date" asp-format="{0:dd/MM/yyyy HH:mm}" readonly class="form-control" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.