MVC and EditorFor width
Asked Answered
C

13

28

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...

Cholon answered 18/1, 2011 at 5:55 Comment(0)
M
38

Instead of EditorFor, use TextBoxFor:

<%=Html.TextBoxFor(m => m.Name, new {style = "width:50px"})%>
Milano answered 5/7, 2011 at 14:58 Comment(1)
That's not a full proof solution. What if we want the control to be a calendar control? TextBoxFor will not render dynamic Field.Ruffin
J
18

What's wrong with using CSS to style your control width?

Jovian answered 18/1, 2011 at 6:22 Comment(4)
That sounds good - but, how? I'd like to have it as a CSS, to have a generic setting for my controls.Cholon
@cdotlister - Try this: <%= Html.EditorFor(m => m.Name, new { @class = "myCss" })%> then in your .css file have input.myCss {width: 50em;}Jovian
I did that, but the control renders like this: <input class="text-box single-line" id="Name" name="Name" type="text" value="Bald Hills Medical Practice" />Cholon
@cdotlister - Oh, sorry, didn't pay close enough attention. EditorFor uses a template to create the class, so you don't have control over the attributes unless you create your own Editor template. Use TextBoxFor and LabelFor to do it yourself, and that will accept the new parameter. Otherwise, you will have to create a custom editor template.Jovian
U
10

In mvc 5 there is setting in site.css that sets the max-width=200 for all textareas. That confused me until i found this blogpost. http://weblogs.asp.net/paullitwin/visual-studio-2013-asp-net-mvc-5-scaffolded-controls-and-bootstrap as Paul Litwin puts it:

Yes, Microsoft is for some reason setting the maximum width of all input, select, and textarea controls to 280 pixels. Not sure the motivation behind this, but until you change this or overrride this by assigning the form controls to some other CSS class, your controls will never be able to be wider than 280px.

/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
    max-width: 280px;
}

So you if you are a pragmatic you change the max-width to eg 600px

Usurp answered 24/6, 2015 at 20:49 Comment(3)
I searched "280 pixel limit on text boxes" and similar things for MVC and couldn't find much until I found this buried comment. This was incredibly frustrating just to find it here :( Thanks for the right direction, upping this max limit has allowed me to actually design my site :)Ransome
I wouldn't imagine that textarea class could reflect an input element, but yeah. That did it!Danaides
@Danaides The styling applies to tags input, select, and textarea. Per CSS rules.Sentimentalism
B
9

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

<%=Html.EditorFor(m => m.Name,new { htmlAttributes = new { style = "width: 50px" }, } 
Bloodroot answered 2/10, 2016 at 18:0 Comment(0)
L
7

With BootStrap 3

@Html.EditorFor(model => model.PriceIndicatorDesc, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" } })
Letendre answered 17/3, 2016 at 15:30 Comment(0)
M
3

As previously mentioned, the place you want to go is in site.css

input,
select,
textarea {
    max-width: 280px;
}

You can set any default value you want there or remove the block to get the Bootstrap default of 100%. Personally, I added the following options so I could easily make some changes based on the control and field in question:

.form-control-25 {
    max-width: 25%;
}

.form-control-50 {
    max-width: 50%;
}

.form-control-75 {
    max-width: 75%;
}

.form-control-100 {
    max-width: 100%;
}
Molybdate answered 3/3, 2017 at 18:19 Comment(0)
S
2

You may need to add "!important" to the css attribute to ensure that it overrides the default for TextBoxFor e.g. width:50px !important;

Shively answered 11/10, 2011 at 10:57 Comment(0)
B
1

Patrik Lindström is correct with the Max-width.

I was able to get around this by setting the max-width and width

@Html.EditorFor(model => model.Source, new { htmlAttributes = new { @class = "form-control", @placeholder = "Source",  @style = "width: 900px;max-width: 900px;" } })
Basal answered 27/2, 2017 at 14:47 Comment(1)
>>> YES <<<. This is the right answer for the question asked. This works for me: @Html.EditorFor(model=>model.BusinessAddress, new { htmlAttributes = new { @style = "width: 1000px;max-width: 1000px;" } })Haggar
K
0

If you need a custom width outside of a normal CSS rule and you are using Editor Templates you can do

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

Then in your editor template for String add this to your template

@Html.TextBox(s => {
...
    if (ViewBag.Width != null )
    {
       s.Width = ViewBag.Width;
    }
 }).Bind(Model).GetHtml()
Kick answered 24/4, 2013 at 22:27 Comment(0)
E
0

In your CSS file for Site.css, the default max-width is 280px if you had VS auto generate everything for you when you first created the MVC Project. No matter how large of a width you set your @html.editorfor with a given id, it is limited by the below syntax.

input,
select,
textarea{
     max-width:280px
}

You can change it here by giving it a new max-width or max-height.

Eburnation answered 9/10, 2018 at 13:1 Comment(0)
F
0

For ASP.NET Core:

<%=Html.TextBoxFor(m => m.Name, new { htmlAttributes = new { style = "width: 50px" } })%>
Formfitting answered 2/2, 2019 at 19:21 Comment(0)
E
0

ASP.Net MVC5

@Html.EditorFor(model => model.ReplaceGroup, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly", @style = "width:450px"  } })
Endomorphic answered 17/1 at 7:48 Comment(2)
Code-only answers aren't very useful. When answering a question, it's good to add a description of why your answer is applicable, so the original poster and other readers can learn why you chose your solution.Sentimentalism
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Velutinous
B
-1

Above answer Yes and No. We need to use develop tool to check what styles used and modify them, they may have max-width, width; Then create own !important css to apply it, my case:

<style>
    textarea,
    input[type='text'],
    .form-group,
    .form-group-lg,
    .form-horizontal,
    .form-control,     
    .text-box,
    .single-line,
    .multi-line{
        width: 800px !important;
        max-width: 1000px !important;
    }
    .multi-line{
        height: 300px !important;
    }
    .form-horizontal{
        position:absolute;
        padding-left:15%;
    }
</style>

see attached image result.

enter image description here

Bugger answered 7/9, 2016 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.