Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET

ASP.NET MVC Web App on 3-Tier for Beginners – Part 4

Rate me:
Please Sign up or sign in to vote.
4.87/5 (15 votes)
13 Jul 2015CPOL4 min read 27.2K   24   12
In this article, let us try to understand and implement master layout and Bootstrap.

Introduction

This article is the continuation of Part 1, Part 2 and Part 3.

In the first article, we focused on understanding requirements and breaking them into objects, then finding out the relationships among them, and finally, designing and implementing the database. And (good news!) our first article won the best article of the Nov 2014 award.

In the second article, we saw how to create a solution, add multiple projects to it, and add desired references to their respective projects. So, finally we are done with our Business Object Layer.

In the third article, we saw how to implement our UI (User Interface). In this article, let us try to understand and implement master layout and bootstrap.

3: Implementing All the Layers (Master layout and Bootstrap)

Our next work item or task is adding bootstrap feature.

Image 1

Let us observe an option whenever I was trying to add View, i.e., It was asking to use a layout.

Image 2

Image 3

It means that it was creating a separate layout for each area.

Layout (in MVC) is nothing but a type of MasterPage (in ASPX webforms). It is creating separate Master Pages for each Area and I don’t want to have separate layout for each area. I want to have a common layout for all the Areas. Let us observe that first.

If you look into Shared folder -> layout as shown:

Image 4

This is the layout you have in Areas -> Admin -> Views -> Shared -> _layout.cshtml.

HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    <link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
    <link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <script src="~/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" 
			data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application name", "Index", "Home", null, 
			new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                </ul>
            </div>
        </div>
    </div>

    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>© @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
</body>
</html>

It is set in ViewStart for each area.

Image 5

In the same sense, in Common, if you observe, you’ll have the same exact layout and it is setting it in ViewStart.

Image 6

In the same way, if you look into Security, you’ll have the same layout and setting it in ViewStart.

Image 7

Also the same in User:

Image 8

Now I want to push layout to the Common view folder that I have outside the Area:

Add a new folder in Views and name it "Shared".

Image 9

I’ll drag one of the layouts and place it in Shared.

Image 10

Image 11

In ViewStart, I’ll just remove the "/Areas/User" i.e., it is going to use the share layout of your common Views folder.

Image 12

Same way do for all the Areas (Admin, Common, and Security). So each view is now pointing to this layout MasterPage.

I can name my Application Name as "LinkHub".

HTML
@Html.ActionLink("LinkHub", "Index", "Home", 
    new { Area = "Common" }, new { @class = "navbar-brand" })

Whenever I click LinkHub, it should take me to Index Home as mentioned in the above statement.

So I need to pass one more parameter for Area. Whenever I click on LinkHub, it will take me to Common Area and Home Controller and Action Index. Save all and execute and Browse for URL /Common/Home. If I click on LinkHub, it will be redirecting to the same page.

Image 13

As we know, the default controller is Home. Now, how do we set the default Area? I want the default to redirect me to controller Home of Common Area.

Image 14

It is very simple to do that. I need to edit the route table and change the default Area.

Image 15

Look for the area and change it to Common.

C#
public class RouteConfig
  {
      public static void RegisterRoutes(RouteCollection routes)
      {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

          routes.MapRoute(
              name: "Default",
              url: "{controller}/{action}/{id}",
              defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional }
          );

          (RouteTable.Routes[routes.Count - 1] as Route).DataTokens["area"] = "Common";
      }
  }

Save and Execute. By default, it should redirect to Common -> Home Controller.

Image 16

This is how you can set your default Area. Now let’s switch to layout. I want links to all the controller indexes or all the links. Notice that you have a div tag with unordered list and in this I’m adding list items.

For example, Home, which is pointing to the Controller Home of Area Common.

I’m adding all the links:

Image 17

Now, let me execute this. And I get my application name Home, Category, ListCategory.

Image 18

Image 19

Image 20

Image 21

Submit URL: We created this form in our earlier article.

Image 22

Image 23

Image 24

Image 25

Image 26

I’ve linked all the pages, but how about UI? It’s not looking good. Here comes the power of your Bootstrap. I need to just right click on the LinkHubUI project in solution and say Manage NuGet Packages.

Image 27

We need to look online so it requires an internet connection search for bootstrap. Then I’ll select Bootstrap for MVC 5.1 and Install. It will install Bootstrap. Read and accept the agreement and say close.

Image 28

Now if you execute, you should see the magic. It should turn into a beautiful UI.

Image 29

For Submit URL, you should see an awesome form with good dropdown.

Image 30

Now, I’m going to show you one more power of Bootstrap -- it is responsive. For any kind of screen resolution, it is going to work or for any screen size. If I’m trying to access my page on my mobile, try checking adjusting the browser window size and see that it will adjust all the link items automatically.

Finally, it has turned into a beautiful Menu.

Image 31

This is a responsive design that I get with the help of Bootstrap. That’s it for this article.

Image 32

Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder ManzoorTheTrainer.com
India India
Manzoor is a Microsoft Certified Trainer who has been working on MS .Net technologies for more than a decade. Apart from development he is also passionate about delivering training on various MS .Net technologies and he has 10+ years of experience as a software development teacher. He writes articles for code-project as well. His YouTube channel has 1 million hits. He is the founder of ManzoorTheTrainer portal.

"I focus on simplifying, complex concepts..." - ManzoorTheTrainer

Founder of www.ManzoorTheTrainer.com [Free .net video tutorials on MS SQL Server, Asp.Net, C#.Net, Ado.Net, Entity Framework, MVC, Web Services, Android]

Comments and Discussions

 
GeneralThanks for sharing Pin
Alireza_136216-Aug-16 10:45
Alireza_136216-Aug-16 10:45 
QuestionIs there any continuation (part 5 etc.) of this article? Pin
K_Sarkar16-Apr-16 3:24
K_Sarkar16-Apr-16 3:24 
QuestionController and Actions Pin
fpaivinha2-Mar-16 13:26
fpaivinha2-Mar-16 13:26 
Questionlink to part 5 Pin
othman daid13-Dec-15 1:45
othman daid13-Dec-15 1:45 
Questioncode project Pin
Member 1194156328-Aug-15 6:40
Member 1194156328-Aug-15 6:40 
Questioncan you please upload the code to download Pin
smarteez177-Aug-15 5:13
smarteez177-Aug-15 5:13 
QuestionAm I missing something Pin
groverwsm20-Jul-15 9:15
groverwsm20-Jul-15 9:15 
AnswerRe: Am I missing something Pin
Mohd Manzoor Ahmed20-Jul-15 9:31
professionalMohd Manzoor Ahmed20-Jul-15 9:31 
GeneralRe: Am I missing something Pin
Member 1200885724-Sep-15 2:31
Member 1200885724-Sep-15 2:31 
GeneralRe: Am I missing something Pin
Member 1234858324-Feb-16 1:24
Member 1234858324-Feb-16 1:24 
QuestionWhere is the link for Part 1, Part 2 and Part 3 article Pin
Tridip Bhattacharjee13-Jul-15 21:49
professionalTridip Bhattacharjee13-Jul-15 21:49 
AnswerRe: Where is the link for Part 1, Part 2 and Part 3 article Pin
Mohd Manzoor Ahmed13-Jul-15 21:53
professionalMohd Manzoor Ahmed13-Jul-15 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.