Craig Craig - 2 months ago 19
C# Question

MVC and EditorFor width

Can I set the width of an EditorFor control on my View?

I have set a few parameters:

[Required, DisplayName("Payee Name"), StringLength(50)]
public string Name { get; set; }


However, I can't seem to set the width of the textbox that gets rendered.

<table width="300" border="0" cellpadding="3" cellspacing="0">
<tr>
<td>
<%=Html.LabelFor(m => m.Name)%>
</td>
<td>
<%=Html.EditorFor(m => m.Name)%>
</td>
</tr>


Can this be done somehow?

I tried:

<%=Html.EditorFor(m => m.Name, new {width=50)%>


But no joy...

Answer

Instead of EditorFor, use TextBoxFor:

<%=Html.TextBoxFor(m => m.Name, new {style = "width:50px"})%>