Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the width of the text box when using Html.TextBoxFor

I have the following line in my view:

<div class="editor-field">      <%= Html.TextBoxFor(m => m.Description)%>                     </div> 

How do I define the width of the text box?

like image 849
AwkwardCoder Avatar asked Mar 25 '10 16:03

AwkwardCoder


People also ask

What is the difference between HTML TextBox and HTML TextBoxFor?

Both of them provide the same HTML output, “HTML. TextBoxFor” is strongly typed while “HTML. TextBox” isn't. Below is a simple HTML code which just creates a simple textbox with “CustomerCode” as name.

How do I use TextBoxFor in HTML?

The HtmlHelper class includes two extension methods TextBox() and TextBoxFor<TModel, TProperty>() that renders the HTML textbox control <input type="text"> in the razor view. It is recommended to use the generic TextBoxFor<TModel, TProperty>() method, which is less error prons and performs fast.


2 Answers

This worked for me for the one-offs, pretty verbose though:

<%: Html.TextBoxFor(m => m.Description, new { style="width:50px;" })%> 
like image 129
Josh Avatar answered Sep 30 '22 18:09

Josh


A reusable solution is to set the width in your css file

.wide {      width: 300px; } 

then set it using the markup above

<%= Html.TextBoxFor(m => m.Description, new { @class= "wide" })%>  
like image 20
Cheddar Avatar answered Sep 30 '22 17:09

Cheddar