Click here to Skip to main content
15,885,860 members
Articles / Web Development / HTML

Durandal View´s with Razor and C# ViewModel - Update 2015

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Jan 2015CPOL2 min read 13.3K   139   7  
Using Razor for SPA View´s

Introduction

In an older article http://www.codeproject.com/script/Articles/MemberArticles.aspx?amid=9559190. I showed how to use razor cshtml views with durandal. After a year, we have a somewhat "better" way to achieve this now.
While the old way is still working and useful for some purposes, there´s also a way to directly map controller actions to durandal views instead of the "generic" action i described in the older article.

Using the code

The ViewLocator of Durandal can be called with some parameters to tell durandal where to look for view files. We can use this to solve our problem.

Step one, change Durandal´s behaviour for views
First we´ll take a look at main.js and look for this:

C#
define(['durandal/app', 
'durandal/viewLocator',
'durandal/system', 
'plugins/router', 
'services/logger'], boot);

function boot(app, viewLocator,viewEngine, system, router, logger) {
app.start().then(function () {
   viewLocator.useConvention();
});
[...].

This tells durandal to use the "default convention" for views e.g. /app/views/*.html

To tell durandal to use something else we now change this behaviour:

C#
// Add "durandal/viewEngine" to the function definition:
define(['durandal/app',
        'durandal/viewLocator',
        'durandal/viewEngine',
        'durandal/system',
        'plugins/router',
        'services/logger'], boot);

// change viewLocator initalisation:
app.start().then(function () {
        viewLocator.useConvention('viewmodels', '../../durandal');
        viewEngine.viewExtension = '/';
});

 

The first argument to viewLocation.useConvention tells durandal to set the /Apps/viewmodels/ directory as the location for the view models js files.
The second parameter however, tells durandal to use the URL http://<mydomain>/durandal/ for the views
Additionally the viewExtension parameter tells durandal that views have no extension (like our mvc controllers...)
Now, when Durandal is looking for the view named 'shell', it will reference http://<mydomain>/durandal/shell/

As of MVC Convention, this URL will be mapped and the system looks for a controller named "Durandal" and "Shell" as the action name

Add a new Controller

In the next step, we must now add a new controller to handle the actions and routes for our views:

C#
// will render dynamic views for Durandal
public class DurandalController : Controller
{
    public ActionResult Shell()
    {
        return View();
    }

    public ActionResult Home()
    {
        return View();
    }

    public ActionResult Nav()
    {
        return View();
    }

    public ActionResult Details()
    {
        return View();
    }

    public ActionResult Sessions()
    {
        return View();
    }

    public ActionResult Footer()
    {
        return View();
    }
}

Now we need to setup the views

This part is simple, just add a folder "Durandal" below "Views" and copy all *.html files from /App/Views/ to the newly created folder. Rename the files to .cshtml and you´re done.


After this step we´re done and can launch the application.
The result is the normal look of our hot towel project, however with a fully working mvc controller in the background.

 

 

Points of Interest

Both ways, this one and the one discussed in the older article, have their pros and cons. For cms behaviour you might need a more generic action but the one discussed in this example has it´s uses also.

License

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


Written By
Software Developer (Senior) Teamwork.com
Ireland Ireland
I´m a german software engineer, passionate microsoft fan and working with everything .net framework offers.

While working on Integrating Microsoft and other Tools into our Product suite i´ll keep writing articles and share my knowledge with fellow developers.I published a couple of articles in print mediums and now start to write guides and articles for code project.

We´re hiring! just drop me a line!

Comments and Discussions

 
-- There are no messages in this forum --