【问题标题】:How to add css in a htmlhelper?如何在 htmlhelper 中添加 css?
【发布时间】:2016-03-24 14:15:56
【问题描述】:

在asp.net mvc 4开发中,我写了如下代码:

@Html.EditorFor(model =>model.CreateTime,new {@class="form-control"}).   

@Html.EditorFor(model =>model.CreateTime,null,new {@class="form-control"})

这两种方法都不行!当我检查生成的 html 代码时,类元素没有出现。

但是当我写这些代码时,就很好了:

@Html.LabelFor(model =>model.CreateTime,new {@class="form-control"})

那么告诉我该怎么做?

【问题讨论】:

标签: asp.net-mvc-4


【解决方案1】:

@Html.EditorFor() 不支持 HTML 属性。而是使用

@Html.TextBoxFor(model =>model.CreateTime,new {@class="form-control"})

或者如果你想使用@Html.EditorFor() 那么

@Html.EditorFor(model =>model.CreateTime,"formcontrol",null)

CSS-

<style>
.formcontrol{
// your Code
}
</style>

尚未测试,但可以开始使用。

已编辑

或者使用jquery

$(document).ready(function () {
   $('#CreateTime').addClass('form-control');
});

【讨论】:

    【解决方案2】:

    @EditorFor 只是生成带有 class=单行的文本框。 尝试改用@Html.TextBoxFor

    【讨论】:

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