我想使用EditorFor作为DateTime字段,因为那时我可以使用数据注释来描述格式(我不想包含时间部分)。我还想分配一个html类属性(class =“date”)。 当我使用EditorFor helper函数时,我无法分配html属性。 当我使用EditorFor并创建编辑器模板时,我可以在模板中指定类,但后来我丢失了格式。这就是我的DateTime编辑器模板的样子:
@ModelType DateTime
@Html.TextBox("", Model, New With {.class = "date"})
如何使这项工作,所以数据注释被考虑在内,所以我可以添加类属性?我谈到的注释是:
<Display(Name:="Task start date")>
<DataType(DataType.Date)>
<DisplayFormat(DataFormatString:="{0:MM/dd/yyyy}", ApplyFormatInEditMode:=True)>
答案 0 :(得分:2)
为了让DataAnnotations流通,您必须更改编辑器模板以使用TextBoxFor
而不是TextBox
,如下所示:
@ModelType DateTime
@Html.TextBoxFor(Function(m) m, New With {.class = "date"})