2014-01-22 31 views
1

为了修改您在使用@Html.EditorFor(m => m.SomeAttribute)时显示的样式,我想知道如何覆盖默认CSS类,而无需为模型的每个属性数据类型声明新类。换一种说法。MVC:哪个CSS类实现Html.EditorFor()方法?

我不想这样做:

@Html.LabelFor(m => m.ddlStationId, new {@class="label"}) 
    @Html.EditorFor(m => m.ddlStationId, new { ReadMethod = "Read", ReadController = "Receiver",@class="combobox" }) 
    @Html.EditorFor(m => m.txbBrandName,new {@class="textbox"}) 

大家都知道,EditorFor()具有依赖于数据类型的不同行为,所以我想知道位于这个类。

我通过使用UIHint类作为装饰器并定义了EditorTemplate,但不允许我重写主CSS。重要的是要说我正在使用Kendo UI框架。一些额外的信息可能是相关的。

回答

0

谢谢汤米!我做了这样的事情,但使用UIhint ......所以,当我声明一个属性时,我根据数据类型设置了一些装饰器,并且在那里使用了Css类。例如:

[Required] 
[Display(Name = "Nombre")] 
[UIHint("String")] 
public string txbName 
{ 
    get; 
    set; 
} 

而且,在编辑模板:

@model object 

@Html.TextBoxFor(model => model, new {@class="k-textbox",style="min-width:200px;" }) 

最后,CSS类 “K-文本框”

.controls-row .k-textbox { 
    margin: 0 auto; 
    clear: left; 
    display: inline-block; 
} 

的定义我preffer这个解决方案,因为允许我将这个标记应用于解决方案中的所有文本框...对于我的具体情况是我想要做的:

<div class="controls-row"> 
    @Html.LabelFor(model => model.txbName) //it shows "Nombre" 
    @Html.EditorFor(m => m.txbName) //it declares an editor for string type 
</div> 

感谢您的回答! :)

2

除非您覆盖默认模板,否则不存在“默认”类。根据您的DataAnnotations,行为可能会与所显示的值不同(格式化字符串,也许您有一个将日期选择器放在文本输入上的模板),但以下各项的输出在MVC中保持一致。在HTML

<label for="Property">Property Value</label> 

@Html.LabelFor(x=>x.Property) 

输出对于一个字符串属性在HTML

@Html.EditorFor(x=>x.SomeStringProperty) 

输出

<input type="text" id="SomeStringProperty" name="SomeStringProperty" value=""/> 

最后,布尔属性

@Html.EditorFor(x=>x.SomeBoolProperty) 

输出在HTML

<input type="checkbox" id="SomeBoolProperty" name="SomeBoolProperty"/> 

鉴于此,然后在您的样式表,您需要创建不是基于class和id的顶级HTML元素的类。这样的例子可能是

<style> 
    <!--apply to all labels --> 
    label { position: absolute; text-align:right; width:130px; } 
    <!--apply to all labels with class check--> 
    label.check { font-weight:bold;padding-left:10px } 
    <!-- apply to all input and textarea tags--> 
    input, textarea { margin-left: 140px; } 
    <!-- apply to all input with class special--> 
    input.special { font-weight: bold } 
</style> 

如果您在配备了一个基本的MVC模板默认的样式表看,你可能会看到的东西相似,我已经张贴以上。您可以使用您的自定义类进行编辑。

相关问题