Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify size and maxlength for Html.TextBoxFor

People also ask

How do I set the MaxLength for HTML TextBoxFor in MVC?

The TextBox for the Name value is created using Html. TextBoxFor function while the TextBox for the Mobile Number value is created using Html. TextBox helper function. The MaxLength of both the TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html.

What is the difference between HTML TextBox HTML TextBoxFor and HTML EditorFor?

Show activity on this post. TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control. EditorFor: This control is bit smart.

How do you make a TextBoxFor ReadOnly?

The TextBoxes can be made ReadOnly by setting the HTML ReadOnly attribute using the HtmlAttributes parameter in Html. TextBox and Html. TextBoxFor helper functions.

What is TextBoxFor in MVC?

TextBoxFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.


You can try this too :

@Html.SimpleTextBoxFor(m => ((ModifiableProperty<string>)m).Value.TheCurrentValue, new { id = fieldId, style ="width:200px"})

Just change the 200px value for the size you want.

For maxlength I use the same syntax as you and it is working for me.

 @Html.TextBoxFor(model => model.EL_Taille_Initiale, new { style = "width:50px", @maxlength = "5" })

Take out the "@" character for your maxlength attribute. You only need that for reserved keywords (i.e. class). Also, you don't need the quotes around the number for maxlength.

@Html.SimpleTextBoxFor(m => ((ModifiableProperty<string>)m).Value.TheCurrentValue, new { id = fieldId, maxlength = 100 })

If that doesn't solve the problem, then please post what the HTML markup is being generated on the response page.


I am using a constructor for my TextBox that does not allow passing HTML attributes, so I had to add this to my $(document).ready function: $('#textBoxID').attr('maxlength', 30); Doesn't directly answer the OP question, but offers an alternate starting point.