如何在ASP.NET MVC中设置HTML Helper文本框的宽度?

问题描述:

一些例子,我发现显然与旧版本的MVC的工作表明,有各种各样的长度参数:如何在ASP.NET MVC中设置HTML Helper文本框的宽度?

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

但是,这可能是错误地设置值。

这个工作在当前版本中是如何工作的?传递风格似乎没有任何效果。

原来的答案不再担任记:

<%=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

像这样的东西应该工作:

<%=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

不要使用长度参数,因为它不会与所有的浏览器。最好的方法是在输入标签上设置样式。

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

对于这一点,你必须使用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" }) 

新{风格= “宽度:50像素”,MAXSIZE = 50};

应该是

新{风格= “宽度:50像素”,最大长度= 50};

我强烈推荐构建你的HtmlHelpers。我认为apsx代码将更具可读性,可靠性和持久性。

请点击链接; http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper.aspx

我对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

设置TextBox与和在mvc3.0

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