Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML5

Convert a MVC4 Application to a Cool Towel Single Page Application (SPA)

2.71/5 (4 votes)
26 Jan 2014CPOL2 min read 22K  
Example project template for a Cool Towel SPA to be filled with a MVC4 application HTML5 and JavaScript code in order to convert
Screenshot running out-of-the-box Cool Towel SPA project template

Introduction 

This tip is about making website navigation just as fast as a desktop application and with the same look and feel, for example in an ASP.NET MVC4 internet application by converting it to a "Cool Towel" Single Page Application (SPA). This is done by putting the HTML5 code of each internet application page in the appropriate places of the SPA project template, also incorporating in this solution the corresponding JavaScript and controller code. A Cool Towel: You do not go to the SPA without one!

Background

A SPA, also known as single-page interface (SPI), is a web application or web site that fits on a single web page with the goal of providing a more fluid user experience akin to a desktop application. In a SPA, either all necessary code – HTML, JavaScript, and CSS – is retrieved with a single page load. The page does not reload at any point in the process, nor does control transfer to another page.

Using the Code

Open the SPA_Project_Template_01 solution in Visual Studio 2012 or later. Copy / paste the HTML5 code of your pages in the "XXXX HTML5 Here" placeholders in the Index.cshtml, add the JavaScript files, add the CSS files and controller files also. Be sure that all the JavaScript and CSS files are referenced in the Index.cshtml

Out-of-the-box the navigation buttons have the click event Knockout data binded (HTML5 code):

HTML
<div class="float-right">
    <nav>
        <ul id="menu1">
            <li>
                <input type="button" value="Home" data-bind="click: $root.divHome" /></li>
            <li>
                <input type="button" value="Manual" data-bind="click: $root.divManual" /></li>
            <li>
                <input type="button" value="Blog" data-bind="click: $root.divBlog" /></li>
            <li>
                <input type="button" value="Balance" data-bind="click: $root.divBalance" /></li>
            <li>
                <input type="button" value="Budget" data-bind="click: $root.divBudget" /></li>
            <li>
                <input type="button" value="About" data-bind="click: $root.divAbout" /></li>
            <li>
                <input type="button" value="Contact" data-bind="click: $root.divContact" /></li>
        </ul>
    </nav>
</div>

Out-of-the-box these click events trigger JQuery opening on collapsing of the placeholder <div>s, like (JavaScript code):

JavaScript
function appViewModel() {

    var self = this;

    self.divHome = function () {
        $("#divHome").show();
        $("#divManual").hide();
        $("#divBlog").hide();
        $("#divBalance").hide();
        $("#divBudget").hide();
        $("#divAbout").hide();
        $("#divContact").hide();
        $("#divTerms").hide();
        $("#divPrivacy").hide();
    }

Points of Interest

Navigation of the budget.satoconor.com demo website that took about 2 seconds according to the PageSpeed Chrome pluggin now is like instantaneous in a view microseconds. Moreover the bootstrap time of the entire SPA Index.cshtml, when application pool and website IIS are alive, has only doubled, from about 2 to 4 seconds, compared to loading the HOME page only of the MVC4 Internet Application.

History

  • 25th January, 2014: Initial version

License

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