简体   繁体   English

指定“asp-for”HTML标记的格式(ASP.NET Core)

[英]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.

相关问题 ASP.NET 核心 -&gt; Label asp-for 不返回 label 文本 - ASP.NET Core -> Label asp-for not returning label text 将 model 传递给 asp-for | ASP.net核心 - Passing model to asp-for | ASP.net Core 复选框中的 asp-for 抛出和 asp.net 核心中的错误 - asp-for in checkbox throws and error in asp.net core ASP.NET 核心 - 如何在另一个自定义标签助手中从 asp-for 标签助手获取输入 ID - ASP.NET Core - How to get the input ID from asp-for tag helper in another custom tag helper 带有 .NET Core 的 Material Design Lite 中的 Asp-for 标签助手 - Asp-for tag helper in Material Design Lite with .NET Core 通过 asp-for="..." 将字符串值传递到 ASP.Net 核心中的 Model 无法正常工作 - Passing a String value through asp-for=“…” into a Model in ASP.Net core isn't working the way it should ASP.NET Core MVC:asp-for 正在渲染不完整的 id 和 name 属性 - ASP.NET Core MVC: asp-for is rendering incomplete id and name attributes ASP.NET 核心 6,在 EditorTemplate 上下文中获取自定义 asp-for TagHelper 的元数据 - ASP.NET Core 6, Obtaining Metadata for a custom asp-for TagHelper in the context of an EditorTemplate 将值从“选择asp-for”传递到“ textarea” asp.net核心剃须刀 - Passing value from “select asp-for” to a “textarea” asp.net core razor ASP.NET Core Tag助手和导致的HTML差异 - ASP.NET Core Tag helpers and resulted HTML difference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM