Adding bootstrap to your EPiServer MVC project

twitter-bootstrap-episerverIn this tutorial we will add Twitter Bootstrap to our EPiServer project and we will create some base classes and our first ViewModel. Further I will explain the purpose of these base classes and the need for a ViewModel. Most of this is standard ASP.NET MVC so if you are already familiar with that you can probably skip this post.

Enhance your templates with Twitter Bootstrap

Remember that our original intention was to create a blogging website. Therefor I suggest you download this blogging bootstrap template to get started:

http://startbootstrap.com/template-overviews/blog-post/

After you downloaded it unzip it and you will see three folders:

  • css
  • fonts
  • js

Copy these folders to your Static directory in your EPiServer project.
Next right click on the Views folder and create a new file _Root.cshtml.

Because _Root.cshtml has an underscore prefix, ASP.NET won’t send the page to a browser even if users request it directly.

This will be our base template for our website. Every other page is a partial of this page. Therefor we call it the root view.

The index.html file included in the blog-post.zip file we downloaded earlier is a good starting point for our _Root.cshtml.We have to strip out some code because now the file contains the whole website and it should only contain the base code.

Now lets take a closer look at this file. First notice that we have corrected all the static file that were included like javascripts and css. We used for this the the URL Helper @Url.Content(). What this helper does is convert any ‘virtual’ path to a real path.

Further we added a @RenderBody. This is were we insert the page you are visiting. So for example if you have an ‘About Us’ page then this page template is rendered (included) where we added the RenderBody statement. Of course ‘About Us’ should then be a partial view of _Root.

More information about RenderBody, RenderPage, RenderSection can be found here:
http://www.codeproject.com/Articles/383145/RenderBody-RenderPage-and-RenderSection-methods-in

Next lets open our StartPage/Index.cshtml page that we created in our previous tutorial. When somebody loads this startpage we like it to be rendered inside the _Root template. To make that happen we need to add a @Layout statement to the file and remove all <html>,<head> and <body> tags. Remember that this page will be pasted inside the _Root template where we put the @Renderbody tag so it should not contain any tags like <html> etc.

Now open your startpage in your browser and it should look like this:

Empty EPiServer StartPage

Empty EPiServer StartPage

Before we are ready I want to point out one more thing. ASP.NET MVC supports the use of _ViewStart.cshtml. This is a special file that will be included before every view is rendered. We can use this file to include the _Root view on every view that is loaded instead of putting this code on every view:

So create a new file ~/Views/_ViewStart.cshtml and put the above code in this view and remove it from the StartPage Index.cshtml page. Now refresh the page in your browser, you should see no difference.

More information about _ViewStart.cshtml can be found here:
http://www.jittuu.com/2011/10/28/ViewStart-in-MVC-3/

 

Base classes

Next lets talk about base classes. If you have taken a look at the Alloy Sample page you probably noticed that every page controller inherits from a PageControllerBase class. The idea behind using base controller classes is to put functionality in here that should be available by all page controllers like action filters for output caching or a logout method.

The same applies to base classes for (page type) models. If you check the Alloy Sample page you will see that most page type models inherit from SitePageData. For page type models its interesting to use base classes for global properties like page meta data.

ViewModels

A view model represents only the data that you want to display on your view/page. But it can also contain some (auto)generated data like a concatenation of firstname and lastname properties into a single name property. Creating and using a ViewModel is easy.

ViewModels should be placed in the directory ~/Models/ViewModels and should have names like this StartPageModel.cs. An example of the use of a ViewModel will I give now:

Lets say this is your model (Models/StartPage.cs)

A ViewModel could now look like this (Models/ViewModels/StartPageModel.cs):

Next you need to adjust your view (Views/StartPage/Index.cshtml):

And last you need to pass the correct model to your view from the controller:

Thats it. Rebuild your project and now you should see the amount of blogs property on your StartPage.

This is ofcourse the very basics of ViewModels. I recommend to take a look at the Alloy Sample project how they use ViewModels there.

 

Leave a Comment.