如何在htmlhelper中添加css?

时间:2016-03-24 03:11:09

标签: asp.net-mvc-4

在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"})

那么告诉我该怎么做?

2 个答案:

答案 0 :(得分:0)

@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>

未经测试但需要开始使用。

<强> EDITED

或使用jquery

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

答案 1 :(得分:0)

@EditorFor只需生成带有class = single-line的文本框。 请尝试使用@Html.TextBoxFor