1

I have a mvc 3 app that uses Razor.There are several forms on the site where information is requested from the user. The problem is some of these boxes need to be longer than others.. I am lost on how to change the width of specific editorFor boxes using css.. The below box is an example of one of the boxes...

        <tr>

    <td>@Html.EditorFor(Function(model) model.School_Title)
        @Html.ValidationMessageFor(Function(model) model.School_Title)</td>
    <td> @Html.EditorFor(Function(model) model.Short_Name)
        @Html.ValidationMessageFor(Function(model) model.Short_Name)</td>

  </tr>

Any help would be greatly appreciated...

2 Answers 2

1

This has been answered a few times in different scenarios here on SO. Basically, you cannot add CSS directly to an EditorFor()

I would HIGHLY suggest using Editor Templates. It's definitely the "right" way to style your EditorFor.

You can tell a model property to use an Editor Template in two different ways.

The first (the simplest) is to create an editor template for a certain data type - DateTime for example.
The second way to do it is to set it declaratively in your DataAnnotations by using a UIHint.

Alternatively, if you're hung up on using inline CSS for your Editor, you will need to use TextBoxFor()

@Html.TextBoxFor(Function(model) model.School_Title, New With {.class = "CustomCssAttribute" }))
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help.. I was in the dark about editor templates even existing in mvc 3..
0

School_Title is a text field. Editor for a text field will generate HTML text input element.

HTML input can be styled by adding the following into your site.css

input[type="text"]{
   background-color: gray;
   color: white;
}

This will apply to all text inputs that use site.css stylesheet.

Alternative solution is to apply a CSS class through HTML attributes. For example:

@Html.TextBoxFor(model => model.School_Title, new { @class: "myStyle" })

In your site.css you'll need to add your style:

.myStyle
{
   background-color: gray;
   color: white;
}

You shouldn't be scared of editors. The only way to get used to them, is to write your own editor. Write an editor that creates a div with some styling. Built-in editors for text fields are no different. They promote encapsulation and code re-usability. Eventually you should see a need to write editors for object graphs.

For example, I may have a view model that contains customer details. If I want to create or update customer details, I'll use @Html.EditorFor(model => model, "Customer"), or if I want to display details of a customer, I'll use

@Html.DisplayFor(model => model, "Customer")

4 Comments

The OP says "The problem is some of these boxes need to be longer than others"... which means that a blanket CSS setting will not work
Just noticed your edit. Unfortunately you're wrong in the adding the class to the EditorFor() as it will not work. You have to use an EditorTemplate or a TextBoxFor()
I was aiming to give an example where we can assign a class through EditorFor. You can then change colours, size of the box etc through CSS. I can't remember whether it's EditorFor or TextBoxFor as I'm writing this in notepad, sorry.
EditorFor does not allow this. It has to be done in a template or a TextBoxFor... Please see my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.