【问题标题】:Html.EditorFor ignoring my specified classHtml.EditorFor 忽略我指定的类
【发布时间】:2012-10-23 12:01:31
【问题描述】:

我有一个这样的模型:

public class MyModel {
    public string Name {get; set;}
    public DateTime StartDate {get; set;}
}

然后,在我的 Razor 视图中,我有这个:

@Html.EditorFor(x => x.StartDate, new { @class = "input datepicker" })

然而,输出的 HTML 看起来像这样:

<input class="text-box single-line" data-val="true" 
 data-val-required="The StartDate field is required." id="StartDate" 
 name="StartDate" type="text" value="01/01/0001 00:00:00">

它似乎完全忽略了我的 @class 定义(以及添加它自己的验证内容)

有没有办法让它用我指定的类渲染?

【问题讨论】:

    标签: asp.net-mvc razor html-helper


    【解决方案1】:

    您不能为 EditorFor 设置类。您可以在此模板中有许多不同的标签。所以你需要在编辑器模板中分配类:

    <div>
        @Html.TextBoxForModel(x => x.StartDate, new { @class = "input datepicker" })
    </div>
    

    或者你可以只使用 TextBoxFor:

    @Html.TextBoxFor(x => x.StartDate, new { @class = "input datepicker" }}) 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-11
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2011-04-19
      • 2019-03-07
      • 2016-08-16
      相关资源
      最近更新 更多