2008-11-27 55 views

回答

62

原來的答案不再擔任記:

<%=Html.TextBox("test", new { style="width:50px" })%> 

將讓你與「{風格=」文本框寬度:50像素「}」爲文本內容。

要調整此爲MVC 1.0的當前版本中,使用下面的腳本(注意添加第二個參數的 - 我有我開始爲空白,相應地調整根據您的需要):

<%=Html.TextBox("test", "", new { style="width:50px" })%> 
+2

是的,這是真的 – 2010-03-20 22:43:41

6

像這樣的東西應該工作:

<%=Html.TextBox("test", new { style="width:50px" })%> 

或者更好:

<%=Html.TextBox("test")%> 

<style type="text/css"> 
    input[type="text"] { width:50px; }  
</style> 
+0

豈不#TEST {寬度最大長度: 50px;}工作也一樣嗎? – 2008-11-28 01:42:55

1

不要使用長度參數,因爲它不會與所有的瀏覽器。最好的方法是在輸入標籤上設置樣式。

<input style="width:100px" /> 
40

對於這一點,你必須使用HtmlAttributes,但有一個問題:HtmlAttributes and css class

,你可以將其定義是這樣的:

new { Attrubute="Value", AttributeTwo = IntegerValue, @class="className" }; 

,這裏是一個更現實的例子:

new { style="width:50px" }; 
new { style="width:50px", maxsize = 50 }; 
new {size=30, @class="required"} 

終於在:

MVC 1

<%= Html.TextBox("test", new { style="width:50px" }) %> 

MVC 2

<%= Html.TextBox("test", null, new { style="width:50px" }) %> 

MVC 3

@Html.TextBox("test", null, new { style="width:50px" }) 
0

新{風格= 「寬度:50像素」,MAXSIZE = 50};

應該是

新{風格= 「寬度:50像素」,最大長度= 50};

1

我對MVC 3實際樣品是在這裏:

<% using (Html.BeginForm()) { %> 

<p> 

Start Date: <%: Html.TextBox("datepicker1", DateTime.Now.ToString("MM/dd/yyyy"), new { style = "width:80px;", maxlength = 10 })%> 

End Date: <%: Html.TextBox("datepicker2", DateTime.Now.ToString("MM/dd/yyyy"), new { style = "width:80px;", maxlength = 10 })%> 

<input type="submit" name="btnSubmit" value="Search" /> 

</p> 

<% } %> 

因此,我們有以下

「DATEPICKER1」 - ID控制

的日期時間。 Now.ToString(「MM/dd/yyyy」) - 默認值爲

style =「width:80px;「 - 文本框的寬度

的maxlength = 10 - 我們可以輸入10個字符僅

+0

MVC 4仍然沒有出來,這是一年前發佈*引發眉毛*。 – Doomsknight 2012-04-04 10:31:28

0

設置TextBox與和在mvc3.0

@Html.TextBoxFor(model => model.SearchUrl, new { style = "width:650px;",maxlength = 250 }) 
相關問題