“asp-format”未应用于标记助手
我在mvc 6项目中使用带有taghelper元素的“asp-format”标签时遇到了问题。
我们的想法是以这种方式格式化日期输入元素:
这个“StartDate”属性在我的模型中,以这种方式声明:
public DateTime StartDate {get; set; }
出于一个奇怪的原因,这个元素永远不会被格式化,并且总是这样呈现:
---> 02/29/2016 00:00:00
所以我创建了一个viewmodel类并定义了一个属性来保存整个人模型。
public class PersonViewModel { public Person Johndoe {get; set; } }
在视图中使用此类,格式化工作。
---> 29/02/2016
您可以在模型本身中提供格式
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] public DateTime StartDate {get; set; }
在你看来简直就像
@Html.EditorFor(model=>model.StartTime)
2)您也可以在不提供模型类的日期格式的情况下执行此操作
@Html.TextBoxFor(m => m.StartDate, "{0:dd/MM/yyyy}")