2012-08-09 120 views

回答

1

試試這個,這應該工作..

@Html.TextBox("Name", new { @class= "mySize" }) 

.mySize 
{ 
    width: 500px; 
} 

也是在你的代碼,嘗試添加一個分號,看看是否能工程,像這樣

@Html.TextBox("Name", new { style = "width:500px;" }) 
0

我很驚訝@亞瑟作品給出的答案。由於這些擴展方法可能會帶有幾個匿名對象的函數,所以很容易在不經意間使用錯誤的對象。

From MSDN,它看起來像你調用此方法:

public static MvcHtmlString TextBox(
this HtmlHelper htmlHelper, 
string name, 
Object value) 

其中值是:

文本輸入元素的值。如果此值爲null,則元素的值將從ViewDataDictionary對象中檢索。如果不存在值,則從ModelStateDictionary對象中檢索該值。

所以value被用來填充輸入。相反,我認爲你要this extension

public static MvcHtmlString TextBox(
this HtmlHelper htmlHelper, 
string name, 
Object value, 
Object htmlAttributes) 

然後,使用這樣的(傳遞null值)內聯樣式添加到標記:

@Html.TextBox("Name", null, new { style = "width:500px;" }) 

或者:

@Html.TextBox("Name", null, new { @class = "myStyle" }) 
相關問題