Reader small image

You're reading from  Bootstrap for ASP.NET MVC - Second Edition

Product typeBook
Published inSep 2016
Reading LevelIntermediate
Publisher
ISBN-139781785889479
Edition2nd Edition
Languages
Right arrow
Author (1)
Pieter van der Westhuizen
Pieter van der Westhuizen
author image
Pieter van der Westhuizen

Pieter van der Westhuizen is a freelance software and web developer specializing in ASP.NET MVC, web technologies, and MS Office development. He started his career in web development using classic ASP, Visual InterDev, HoTMetaL, and FrontPage. Pieter has over 16 years of experience in the IT industry and is also one of the people fortunate enough to have his hobby become his full-time profession. He is also a technology evangelist for Add-in Express (www.add-in-express.com), which focuses on tools for Microsoft Office integration. This is Pieter's second book and he has been blogging since 2007 on his personal blog at www.mythicalmanmoth.com and on the Add-in Express blog since 2010. He lives with his wife and two dogs in Pretoria, South Africa.
Read more about Pieter van der Westhuizen

Right arrow

Chapter 6.  Converting a Bootstrap HTML Template into a Usable ASP.NET MVC Project

One of the major benefits of using Bootstrap is the wide variety of resources available on the Internet. The web development community has embraced Bootstrap, and you'll find tons of valuable templates, snippets, and advice on using Bootstrap online.

By combining a predesigned Bootstrap template and ASP.NET MVC, you can save a lot of time without having to worry about site layout or design.

In this chapter, we will cover the following topics:

  • Why we use prebuilt HTML templates and how they will save time

  • Building the master layout

  • Adding specific page views

  • Including charts in your views

Working with prebuilt HTML templates


It is a well-known fact that most developers are not necessarily good designers. We prefer to work on the backend, building great performing and intelligent software, and sometimes, we tend to think of the user interface as an afterthought.

By using a predesigned HTML Bootstrap template, we can give our users an intuitive and well-designed user interface that was designed by a professional designer. If the design was based on Bootstrap, the developer is already familiar with most of the CSS class names, components, and plugins, and does not have to relearn anything.

The Web offers an assortment of free and premium Bootstrap templates. Themeforest (www.themeforest.net) provides a mind-boggling array of different premium site styles and designs.

With Bootstrap 4, the Bootstrap team also offers official themes, which you can purchase. Each theme provides a full set of tools and examples and can be used as a good starting point for your project.

Note

The Bootstrap...

Creating the ASP.NET MVC project


To create a new ASP.NET MVC project, perform the following steps:

  1. In Visual Studio, create a new ASP.NET Core Web Application project, as shown in the following screenshot:

  2. In the New ASP.NET Core Web Application dialog, select the Empty template under the ASP.NET Core Templates and click on the OK button:

  3. Visual Studio will create a default empty MVC project. Right-click on the wwwroot folder inside the project and navigate to Add | New Folder. Create the following two folders:

    • css 

    • js

  4. Add the styles.css file from the Bootstrap 4 Admin Dashboard template css folder to the css folder inside the wwwroot folder in the project.

  5. Copy the scripts.js file located in the Bootstrap 4 Admin Dashboard template's js folder to the js folder in the project's wwwroot folder.

  6. Next, add two folders to the root of the project called Controllers and Views.

  7. Next, in order to enable MVC features such as tooling and Tag Helpers, open the project.json file and add the following to the...

Creating the master layout


You've added the CSS and JavaScript files needed to create the master layout file for your project. Next, you need to create a home controller as well as a master layout file. To do this, complete the following steps:

  1. Add a new empty controller called HomeController to the Controllers folder by right-clicking on it and selecting Add | New Item....

  2. Select MVC Controllers Class from the list of project items and click on the Add button.

  3. Next, right-click on the Views folder in your project and navigate to Add | New Folder. Name the folder Shared.

  4. Right-click on the newly created Shared folder and navigate to Add | New Item...

  5. Select MVC View Layout Page in the list of project items and keep the name of the file as _Layout.cshtml and click on Add.

  6. Open the index.html file in the Bootstrap 4 Admin Dashboard template source files and copy its contents to the _Layout.cshtml file.

  7. Change the <head> tag to reference the styles.css folder in the correct folder, as illustrated...

Adding a view for the home controller


You need to create a view for the home controller's Index action in order to test the template. Complete the following steps to accomplish this:

  1. Open the HomeController.cs file and, if the HomeController class does not already contain an Index action method, add it as shown here:

            public IActionResult Index() 
            { 
                return View(); 
            } 
    
  2. Next, right-click on the Views\Home folder and Add | New Item... from the context menu.

  3. Select MVC View Page from the list of project items, make sure the name is Index.cshtml, and click on Add:

  4. Open the newly created Index.cshtml file and change its markup to the following:

            <p class="hidden-md-up"> 
                <button type="button" class="btn btn-primary-outline btn-sm" 
                 data-toggle="offcanvas"><i class="fa fa-chevron-left"></i>         
            Menu</button> 
            </p> 
            <h1 class="display-1 hidden-xs-down"> 
            ...

Adding different page views


Next, you'll create a custom view that displays two cards, one with a simple Bootstrap form and another displaying an image, by completing the following steps:

  1. Add a new empty MVC Controller Class called FormsController.cs to the project's Controllers folder.

  2. The new controller class should already contain an Index action method.

  3. Next, add a new folder called Forms inside the Views folder.

  4. Add a new MVC View Page, called Index.cshtml, to the newly created Forms folder.

  5. Add the following markup to the view:

            <div class="row"> 
                <h1>Forms</h1> 
                <div class="col-md-3"> 
                    <form> 
                        <div class="card card-success" style="max-width: 20rem;"> 
                            <div class="card-header"> 
                                First Panel with simple form 
                            </div> 
                            <div class="card-block"> 
                                ...

Adding charts to views


By adding charts to your views, you can provide a rich experience for your users and the ability for them to receive an overall view of important information about the system.

There are a number of options when it comes to adding charts and graphs to your project; some of the most popular charting components are as follows:

Adding Google Charts to views

Google provides a rich charting API, which is powerful, easy to use, and free. They also provide an interactive gallery that showcases their many varieties of available charts.

In order to add Google Charts to the Index view of the Home controller, complete the following steps:

  1. Inside the Visual Studio Solution Explorer, double-click the Index.cshtml file inside the Views\Home folder.

  2. Add a new Bootstrap row to the view, which will act as the container for the Google...

Summary


In this chapter, you've learned how to convert a predesigned HTML template into a usable ASP.NET MVC project. The techniques shown in this chapter can be applied to virtually any HTML template, allowing you to build professionally designed web applications without having to design the layout yourself.

In the next chapter, we'll explore how to include and use the jQuery DataTables plugin in your own ASP.NET MVC projects.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Bootstrap for ASP.NET MVC - Second Edition
Published in: Sep 2016Publisher: ISBN-13: 9781785889479
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Pieter van der Westhuizen

Pieter van der Westhuizen is a freelance software and web developer specializing in ASP.NET MVC, web technologies, and MS Office development. He started his career in web development using classic ASP, Visual InterDev, HoTMetaL, and FrontPage. Pieter has over 16 years of experience in the IT industry and is also one of the people fortunate enough to have his hobby become his full-time profession. He is also a technology evangelist for Add-in Express (www.add-in-express.com), which focuses on tools for Microsoft Office integration. This is Pieter's second book and he has been blogging since 2007 on his personal blog at www.mythicalmanmoth.com and on the Add-in Express blog since 2010. He lives with his wife and two dogs in Pretoria, South Africa.
Read more about Pieter van der Westhuizen