Ciprian Grosu
Ciprian Grosu

Reputation: 2841

MVC 2 EditorForModel specify width of generated input

I use the Html.EditorForModel() in a view to generate fields for the edit form. I also have another partial class where I specify some Data Annotation attributes for some fields (like DisplayName, Range etc).

When I run the application I have HTML inputs generated for each field. How can I specify the width of this generated inputs ?

Something like this:

<input id="nameTextBox" style="width:220px" name="theName" />

Upvotes: 3

Views: 844

Answers (2)

Drew Noakes
Drew Noakes

Reputation: 311305

@Gerrie's solution works, but it applies to all inputs on the page. If you have other inputs on the page that you don't want to style this way, then you can do something like this:

<div id="model-editor">
    <%: Html.EditorForModel() %>
</div>

And then use CSS of this form:

#model-editor input { width:220px; }

EDIT

If you want to set CSS rules for individual fields, you can do so too. Inspect the generated HTML and find the input's id, then do:

input[id='YourInputId'] { width:400px; }

Upvotes: 2

Gerrie Schenck
Gerrie Schenck

Reputation: 22378

use CSS:

input
{
    width: 220px;
}

This way you don't have to bother about inserting these style attributes into the generated controls.

Upvotes: 1

Related Questions