Reputation: 11794
How can I set the width of a textbox in asp.net mvc 3? this does not work:
@Html.TextBox("Name", new { style = "width:500px" })
Upvotes: 0
Views: 855
Reputation: 1661
I'm surprised the answer given by @Yasser works. Since these extension methods are overloaded with functions that can take several anonymous objects, it's easy inadvertently use the wrong one.
From MSDN, it looks like you're calling this method:
public static MvcHtmlString TextBox(
this HtmlHelper htmlHelper,
string name,
Object value)
where value is:
The value of the text input element. If this value is null, the value of the element is retrieved from the ViewDataDictionary object. If no value exists there, the value is retrieved from the ModelStateDictionary object.
So value is used to populate the input. Instead, I think you want this extension:
public static MvcHtmlString TextBox(
this HtmlHelper htmlHelper,
string name,
Object value,
Object htmlAttributes)
Then, use it like this (pass null for value) to add inline styles to the markup:
@Html.TextBox("Name", null, new { style = "width:500px;" })
Or:
@Html.TextBox("Name", null, new { @class = "myStyle" })
Upvotes: 0
Reputation: 47774
try this, this should work..
@Html.TextBox("Name", new { @class= "mySize" })
.mySize
{
width: 500px;
}
also in your code,try adding a semicolon and see if that works, something like this
@Html.TextBox("Name", new { style = "width:500px;" })
Upvotes: 1