marcin
marcin

Reputation: 79

How can I set a size for html.EditorFor helper?

What is the simplest way to set the size of the generated field?

Upvotes: 7

Views: 13257

Answers (2)

Adam Terlson
Adam Terlson

Reputation: 12730

Another option still: rather than appending the class to the wrapper around your input, you can apply HTML properties to the input itself (doesn't work for "EditorFor" though):

@Html.TextBoxFor(x => x.Foo, new { @class = "bar" })

Further discussion: http://michaelware.net/post/2010/01/31/Custom-Attributes-When-Using-HtmlTextBoxFor.aspx

Upvotes: 6

Darin Dimitrov
Darin Dimitrov

Reputation: 1039408

Using CSS:

<div class="foo">
    <%= Html.EditorFor(x => x.Foo) %>
</div>

and in your CSS file:

.foo input {
    width: 200px;
}

You could also implement a custom DataAnnotationsModelMetadataProvider which would allow you to attach any attributes you like to the generated input field such as class, maxlength, size, ...

Upvotes: 15

Related Questions