2014-07-18 26 views
0

我需要使用引導樣式和一些HTML自定義屬性來構建輸入文本區域。這裏是我的代碼:Html.EditorFor如何設置引導類和HTML屬性?

 @Html.EditorFor(model => model.Comments, new { style = "color: red; width: 400px", @class = "form-control", placeholder = "Enter detailed comments..."), type = "text" }) 

我的模型數據來自具有下列數據註釋的視圖模型:

[DataType(DataType.MultilineText)] 
public string Comments{ get; set; } 

不幸的是這doesn't工作,因爲我工作,如果it's一個TextBoxFor。 EditorFor沒有風格,在sceen上顯示一個普通的小默認HTML。

我想知道我該如何解決這個問題?我正在使用MVC4,現在我不能移動到MVC5.1(這是一個要求,我知道MVC5.1在這方面有一些改進)。

回答

2

由於您使用MultilineText屬性,我認爲你想要一個textarea?你可以只在EditorFor調用只是改變TextAreaFor

@Html.TextAreaFor(model => model.Comments, new { style = "color: red; width: 400px", @class = "form-control", placeholder = "Enter detailed comments..."), type = "text" }) 
0

您可以覆蓋default editor template~/Views/Shared/EditorTemplates/MultilineText.cshtml):

<someboostraphtmlelements> 
@Html.TextArea(
    "", 
    ViewData.TemplateInfo.FormattedModelValue.ToString(), 
    ViewData 
) 
</someboostraphtmlelements> 

這是無恥地從Darin Dimitrov Answer被盜過類似的問題。