Book Image

Bootstrap for ASP.NET MVC

By : Pieter van der Westhuizen
Book Image

Bootstrap for ASP.NET MVC

By: Pieter van der Westhuizen

Overview of this book

<p>Bootstrap, a leading open source frontend framework, takes care of typography, form layouts, and user interface components, allowing a developer to focus on writing code. Integrating ASP.NET's powerful components can further enhance its capabilities. This book guides you through the process of creating an ASP.NET MVC website from scratch using Bootstrap.</p> <p>You will learn about various Bootstrap components as well as techniques to include them in your own projects. The book includes practical examples to show you how to use open source plugins with Bootstrap and ASP.NET MVC and will guide you through building a website using Bootstrap, utilizing layout and user interface components. In the process, you will also learn to build HTML helpers and T4 templates as well as how to use the jQuery DataTables plugin. At the end of this book, you will find some valuable tips and tricks, which will help you in getting the most out of your Bootstrap and ASP.NET MVC integrated website.</p>
Table of Contents (18 chapters)
Bootstrap for ASP.NET MVC
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Using Bootstrap with a site created with the standard Visual Studio project template


From Visual Studio 2013, when creating an ASP.NET project, you only have one project template to choose from, that is, the ASP.NET Web Application project template, as shown in the following screenshot:

In the New ASP.NET Project dialog, you have a choice to select the type of ASP.NET web application you would like to create. To create an ASP.NET MVC web app that uses Bootstrap for its styling and layout, select the MVC template. You'll notice that the MVC checkbox is automatically selected, as shown in the following screenshot:

Click on the OK button to finish the creation of the MVC project in Visual Studio. You'll notice that the project template automatically adds a number of NuGet packages to your project, including the Bootstrap NuGet package.

Examining the default MVC project layout

The default project template adds all the necessary Bootstrap files we discussed earlier, although it does not use the same folder naming convention as the default Bootstrap distribution. The default project layout will look similar to the following screenshot:

The Content folder

The Content folder contains both the bootstrap.css and bootstrap.min.css files as well as a style sheet called Site.css. This file is used to apply any additional styling on top of the default styles provided by Bootstrap, and it is also used to specify the styles to use for the jQuery validation plugin required by ASP.NET MVC for form validation. For example, the following CSS highlights any input element with a reddish color and draws a border around the element if the validation for that field failed:

.field-validation-error {
    color: #b94a48;
}

.field-validation-valid {
    display: none;
}

input.input-validation-error {
    border: 1px solid #b94a48;
}

input[type="checkbox"].input-validation-error {
    border: 0 none;
}

.validation-summary-errors {
    color: #b94a48;
}

.validation-summary-valid {
    display: none;
}

The fonts folder

The fonts folder contains the Glyphicon font in all the necessary formats.

The Scripts folder

The Scripts folder contains a number of scripts. Most notably for this book, it contains the bootstrap.js and bootstrap.min.js JavaScript files. The default ASP.NET MVC project template also adds both minified and normal files for the following JavaScript libraries and plugins:

  • jQuery

  • jQuery validation plugin

  • jQuery and jQuery validation support library for unobtrusive validation

  • Modernizr

  • Respond JS

Visual Studio enables intelliSense for jQuery, Bootstrap, and Modernizr as well as responds by adding the _reference.js file to the Scripts folder. This is a very useful feature when working with JavaScript and well worth using when working with the Bootstrap components.

Most of these libraries and files are beyond the scope of this book, but we will touch on some of them as we progress.