Building MVC 4 App With Bootstrap Layout

Prerequisites: VS2013 and MVC 4

First let's create a default MVC4 application using Visual Studio.

In the menu select "File" -> "New" -> "Project..." then in the installed templates seelct Visual C# –> Web then select “ASP.NET MVC 4 Web Application” and name the application MVC_Bootstrap.

Now just run the application and see if everything is working. Sounds good.

Now right-click the MVC_Bootstrap web project and select Manage Nuget Package –> Online then in the top-right search text area enter “bootstrap” and click search. Look for the package created by Twitter, Inc.

The preceding step will add many bootstrap CSS files to the Content folder and many JavaScript files to the Scripts folder. Please verify. Sounds good.

Open the _layout.cshtml file and the following tags in the HEAD section or you can add a proper bundle configuration to the App_Start/BundleConfig.cs file.

  1. <link href="@Url.Content("~/Content/bootstrap.min.css")" rel="stylesheet" type="text/css" />  
  2. <link href="@Url.Content("~/Content/bootstrap-theme.min.css")" rel="stylesheet" type="text/css" />  
  3. <script src="@Url.Content("~/Scripts/bootstrap.min.js")"></script>  
The following are things to know about Bootstrap:

 

  • CSS class “container-fluid”: This is usually applied to the main DIV of the body. This div should hold all the HTML for the proper layout.

  • CSS class “row”: All rows should be applied with this class, this applies the width as a percentage. If you want to go with PX go with row-fluid.

The entire screen width is divided into 12 equal columns (1 to 12).

For our application we will have a header followed by the content as in the following.

  1. First let's fix the _Layout.cshtml page, so open this page and remove all the HTML (header, body and footer) content inside the body tag.

  2. Let's add 3 divs as shown below. So here what we are doing is we are creating a container div and adding 3 divs.

    1. header
    2. body
    3. footer

  3. Each row is embedded with a column of width:
    1. <body>  
    2.     <div class="container-fluid">  
    3.         <div class="row">  
    4.             <div class="col-sm-12 col-md-12 col-lg-12">  
    5.                 <h6> HEADER </h6>  
    6.             </div>  
    7.         </div>  
    8.         <div class="row">  
    9.             <div class="col-sm-12 col-md-12 col-lg-12">  
    10.                 @RenderBody()  
    11.             </div>  
    12.         </div>  
    13.         <div class="row">  
    14.             <div class="col-sm-12 col-md-12 col-lg-12">  
    15.                 <h6> FOOTER </h6>  
    16.             </div>  
    17.                 @Scripts.Render("~/bundles/jquery")  
    18.                 @RenderSection("scripts", required: false)  
    19.   
    20.         </div>  
    21.     </div>  
    22.         @Scripts.Render("~/bundles/jquery")  
    23.         @RenderSection("scripts", required: false)  
    24.   
    25. </body>  
    Now let's add the following action to the HOME controller:
    1. public ActionResult MyForm()  
    2. {  
    3.    return View();  
    4. }  

Add MyForm.cshtml in folder Views ==> Home.

Add the following content to MyForm CSHTML. In this we have divided the column into a sum of 12.

  1. <div class="container-fluid">  
  2.     <div id="MyForm" class="row">  
  3.         <div class="col-sm-12 col-md-12 col-lg-12">  
  4.             <div class="row">  
  5.                 <div class="col-sm-1 col-md-1 col-lg-1">  
  6.                     <div class="labelClass">Name:</div>  
  7.                 </div>  
  8.                 <div class="col-sm-2 col-md-2 col-lg-2">  
  9.                     <select id="User" name="User" style="width: 100%">  
  10.                         <option>Select</option>  
  11.                         <option>User1</option>  
  12.                     </select>  
  13.                 </div>  
  14.                 <div class="col-sm-1 col-md-1 col-lg-1">  
  15.                     <div class="labelClass">Lead Name:</div>  
  16.                 </div>  
  17.                 <div class="col-sm-2 col-md-2 col-lg-2">  
  18.                     <input id="LeadName" name="LeadName" type="text" value="" style="width: 100%">  
  19.                     </div>  
  20.                     <div class="col-sm-1 col-md-1 col-lg-1">  
  21.                         <div class="labelClass">Date:</div>  
  22.                     </div>  
  23.                     <div class="col-sm-2 col-md-2 col-lg-2">  
  24.                         <input type="date" style="width: 100%" />  
  25.                     </div>  
  26.                     <div class="col-sm-1 col-md-1 col-lg-1">  
  27.                         <div class="labelClass">Country:</div>  
  28.                     </div>  
  29.                     <div class="col-sm-2 col-md-2 col-lg-2">  
  30.                         <select id="Country" name="Country" style="width: 100%">  
  31.                             <option>Select</option>  
  32.                             <option>Select 1</option>  
  33.                         </select>  
  34.                     </div>  
  35.                 </div>  
  36.             </div>  
  37.         </div>  
  38.     </div>  
Now when you run this application all the controls will be aligned properly.

Now try to resize the window to as small as you can and you would see that the controls will auto-align themselves one below another. You can also change your screen resolution and verify that.

I hope this was helpful!

Up Next
    Ebook Download
    View all
    Learn
    View all