【问题标题】:Html.EditorFor, data annotations and html attributes for DateTime fieldHtml.EditorFor,DateTime 字段的数据注释和 html 属性
【发布时间】:2011-10-26 09:54:08
【问题描述】:

我想为 DateTime 字段使用 EditorFor,因为这样我可以使用数据注释来描述格式(我不想包括时间部分)。我还想分配一个 html 类属性(class="date")。 当我使用 EditorFor 辅助函数时,我无法分配 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)>

【问题讨论】:

    标签: asp.net-mvc-3 datetime razor data-annotations html


    【解决方案1】:

    为了让 DataAnnotations 流过,您必须将编辑器模板更改为使用 TextBoxFor 而不是 TextBox,如下所示:

    @ModelType DateTime
    @Html.TextBoxFor(Function(m) m, New With {.class = "date"})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多