Visual Studio 2013, ASP.NET MVC 5 Scaffolded Controls, and Bootstrap

A few days ago, I created an ASP.NET MVC 5 project in the brand new Visual Studio 2013. I added some model classes and then proceeded to scaffold a controller class and views using the Entity Framework.

Scaffolding Some Views

ScaffoldedCreateActionVisual Studio 2013, by default, uses the Bootstrap 3 responsive CSS framework. Great; after all, we all want our web sites to be responsive and work well on mobile devices. Here’s an example of a scaffolded Create view as shown in Google Chrome browser

 

Looks pretty good.

Okay, so let’s increase the width of the Title, Description, Address, and Date/Time textboxes. And decrease the width of the  State and MaxActors textbox controls.

Can’t be that hard…

Digging Into the Code

Let’s take a look at the scaffolded Create.cshtml file. Here’s a snippet of code behind the Create view. Pretty simple stuff.

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>RandomAct</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Title, 
new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Description,
new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Description) @Html.ValidationMessageFor(model => model.Description) </div> </div>

A little more digging and I discover that there are three CSS files of importance in how the page is rendered: boostrap.css (and its minimized cohort) and site.css as shown below.

Content

 

The Root of the Problem

And here’s the root of the problem which you’ll find the following CSS in Site.css:

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

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.

The Fix

Okay, so here’s the deal: I hope to become very competent in all things Bootstrap in the near future, but I don’t think you should have to become a Bootstrap guru in order to modify some scaffolded control widths. And you don’t. Here is the solution I came up with:

  1. Find the aforementioned CSS code in SIte.css and change it to something more tenable. Such as:
/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
    max-width: 600px;
}
  1. Because the @Html.EditorFor html helper doesn’t support the passing of HTML attributes, you will need to repalce any @Html.EditorFor() helpers with @Html.TextboxFor(), @Html.TextAreaFor, @Html.CheckBoxFor, etc. helpers, and then add a custom width attribute to each control you wish to modify. Thus, the earlier stretch of code might end up looking like this:
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Random Act</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Title, 
               new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.Title, 
new { style = "width: 400px" })
@Html.ValidationMessageFor(model => model.Title) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Description, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextAreaFor(model => model.Description,
new { style = "width: 400px" })
@Html.ValidationMessageFor(model => model.Description) </div> </div>

Resulting Form

Here’s what the page looks like after the fix:

CreateActionControls2

7 Comments

  • You should use bootstrap classes to handle width, since forcing a px Will kill you responsive design

  • Luis: In general, I agree but unfortunately, given the style sheet settings made in Site.css and the fact that once I pull in Bootstrap styles, I need to understand a lot more, this "quick" fix will make sense for some people who just don't want to become Boostrap gurus to change the width of a few textboxes.

  • I'd say that's pretty poor reasoning, to be honest. You don't need to be a Bootstrap guru to work with the grid layout. To correctly fix your issue, just remove the CSS that MS added completely and control your control widths using the col-md-x class on the div surrounding each control. You could use col-md-1 for things like your state and max actor fields and col-md-10 for wide ones like title - or anything in between to suit. Then you'll have a working layout which is still responsive and avoid having to change all your EditorFor() calls just to add html attributes to every one.

  • I guess I disagree somewhat because there's a lot of potential failure points in your suggested solution. I suggest you create a post to show how.

  • What potential failure points do you see?

  • @Polynomial All those steps you mention are potential points of failure for someone who is not a CSS/Bootstrap guru. That's why I suggest a post laying out each of the steps in detail. Thanks.

  • THANK YOU!!!!

    Gosh this was driving me crazy. And i was trying to get a textarea to occupy full width with form-control class inside a col and div rows with appropriate bootstrap col classes.

    removed the max-width from site.css and it worked.

    Now one wouldnt use site.css in a production site but for prototyping this was driving me insane.

    Thanks again!

Comments have been disabled for this content.