9

I have some modifications to bring to a form which use EditorFor (and using MVC5) and I can't find the way of setting the size of the text box ... I tried the hard way with:

@Html.EditorFor(model => model.Nom, new{@style="width:400px"})

But this did not work.. Is there an easy way?

0

5 Answers 5

15

In MVC up to version 5, EditorFor does not allow you to specify html elements in that way. It can only be used in non-editor contexts, like TextBoxFor, etc...

In MVC 5.1 they added the ability to specify html attributes in Editor templates, and you can now do this:

@Html.EditorFor(model => model, new { htmlAttributes = new { @class = "form-control" }, })
Sign up to request clarification or add additional context in comments.

8 Comments

Yes I tried this but I forgot to mention that the app use Bootstrap3 (perhaps it could bring troubles?) and whatever I changed with this method give no result, no attribute added only the class="text-box single-line" which is the html attibutes redered with the original code: @Html.EditorFor(model => model.Nom)
@AlainBUFERNE - As I said, this doesn't work with MVC5. You need MVC 5.1 for it to work. MVC 5.1 is completely bootstrap 3 compatible.
Ooo, I jump over the .1 detail I will check that asap and let you know. Thanks
Ok, it's working now. I thank you to have insisted when I missed the .1 . Some details can change your life .......
@ErikFunkenbusch This doesn't seem to work. EditorForText resize correctly based on col-md-9 but when I change it to EditorFor the editor is only about half size. I'm using MVC 5.2.3. Any ideas?
|
7

Using MVC 5 - You need to use [DataType(DataType.MultilineText)] attribute on your view model ejm:

using System.ComponentModel.DataAnnotations;

public class Requests
{

    [Display(Name = "Id")]
    public int RequestId { get; set; }

    [DataType(DataType.MultilineText)]
    public string Comments { get; set; }
}

Comments

2

It is not a great idea to apply css class to EditorFor template because an EditorTemplate may can have many elements in that. What you can do is to apply your css thing inside your EditorTempalte file. Checkout this answer for more details.

But if you are simply trying to render a textarea, you may simply use the TextAreaFor helper method.

@Html.TextAreaFor(model => model.Nom, new { @class = "myCustomClass" }) 

and your css

.myCustomClass
{
  width:400px;
}

You may use !IMPORTANT if you specifically want to make sure that this css style overrides the eixsting style.

.myCustomClass
{
  width:400px !IMPORTANT;
}

Comments

1

the same but in VB

@Html.TextBoxFor(Function(model) model.nit , New With {.class = "form-control"})

Comments

0

If you want to apply it to all your EditorFor, just change the max-width in your Site.css from 280px to any other value. Example:

textarea {
/*max-width: 280px;*/
 max-width: 900px;
}

This worked for me in MVC 5.1

Comments

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.