MVC and EditorFor width

纵饮孤独 提交于 2019-12-03 04:31:27

Instead of EditorFor, use TextBoxFor:

<%=Html.TextBoxFor(m => m.Name, new {style = "width:50px"})%>

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

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

Sergio Caretta

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

<%=Html.EditorFor(m => m.Name,new { htmlAttributes = new { style = "width: 50px" }, } 
Joe

With BootStrap 3

@Html.EditorFor(model => model.PriceIndicatorDesc, new { htmlAttributes = new { @class = "form-control", @style = "width:280px" } })

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

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%;
}

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()

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;" } })
GuyverZero

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.

For ASP.NET Core:

<%=Html.TextBoxFor(m => m.Name, new { htmlAttributes = new { style = "width: 50px" } })%>
Allen Song

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!