Client-Side Template in ASP.Net (Web Form / MVC) With Mustache.js

Introduction

The string concatenation approach is an effective way to generate client-side markup for small sections of markup. As the amount of markup grows, it adds complexity to the code that needs to concatenate it, resulting in something that is increasingly difficult to maintain.

Client-side templates are powerful alternatives to simple string concatenation that lets you quickly and effectively transform JSON data into HTML in a very maintainable way. Client-side templates define reusable sections of markup by combining simple HTML with data expressions that can range from a simple placeholder to be replaced with data values, to full-blown JavaScript logic that can perform even more powerful data processing directly within the template.

Mustache (Template System)

Mustache is an open source web template system developed for languages such as ActionScript, C++, Clojure, CoffeeScript, ColdFusion, D, Erlang, Fantom, Go, Java, JavaScript, Lua, .NET, Objective-C, Perl, PHP, Python, Ruby, Scala and XQuery.

You can grab a copy of the library by visiting http://mustache.github.io/. Mustache is “logic-less” template syntax. “Logic-less” means that it doesn’t rely on procedural statements (if, else, for, and so on.): Mustache templates are entirely defined with tags. It is named "Mustache" because of heavy use of curly braces that resemble a mustache. Mustache is used mainly for mobile and web applications.

Mustache.js with ASP.NET Web Form

This section of article will introduce how to use a client-side template on your web form instead of string concatenation. So let’s start. First create a web form that will use string concatenation to display person detail. Create a Person class that will be used to show a list of people on the web.

  1. namespace MustacheTemplate  
  2. {  
  3.     public class Person  
  4.     {  
  5.         public int PersonID { getset; }  
  6.         public string Name { getset; }  
  7.         public bool Registered { getset; }  
  8.     }  
  9. }   
Define a web method that will return a list of people from the code behind page of the web form.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web.Script.Serialization;  
  4. using System.Web.Services;  
  5. namespace MustacheTemplate  
  6. {  
  7.     public partial class ShowTemplate : System.Web.UI.Page  
  8.     {  
  9.         protected void Page_Load(object sender, EventArgs e)  
  10.         {  
  11.         }  
  12.         [WebMethod]  
  13.         public static string GetAllPerson()  
  14.         {  
  15.             var RegisteredUsers = new List<Person>();  
  16.             RegisteredUsers.Add(new Person() { PersonID = 1, Name = "Sandeep Singh",   
  17.             Registered = true });  
  18.             RegisteredUsers.Add(new Person() { PersonID = 2, Name = "Raviender Singh",   
  19.             Registered = false });  
  20.             RegisteredUsers.Add(new Person() { PersonID = 3, Name = "Hameer Singh",   
  21.             Registered = true });  
  22.             RegisteredUsers.Add(new Person() { PersonID = 4, Name = "Kuldepp Singh",   
  23.             Registered = false });  
  24.             JavaScriptSerializer js = new JavaScriptSerializer();  
  25.             return js.Serialize(RegisteredUsers);  
  26.         }  
  27.     }  
  28. }   
Now define the web form that renders the person list on the web. The following code snippet is a web form.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowTemplate.aspx.cs"
  2. Inherits="MustacheTemplate.ShowTemplate" %>
  3. <!DOCTYPE html>
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title></title>
  7.     <script src="Scripts/jquery-2.1.0.min.js"></script>
  8.     <script type="text/javascript">
  9.         $(document).ready(function ()
  10.         {
  11.             var tablePerson = $("#tblPerson");
  12.             var divPerson = $("#divPerson");
  13.             $.ajax({
  14.                 cache: false,
  15.                 type: "POST",
  16.                 url: "ShowTemplate.aspx/GetAllPerson"
  17.                 data: {},
  18.                 contentType: "application/json; charset=utf-8",
  19.                 dataType: "json",
  20.                 success: function (data)
  21.                 {
  22.                     var personList = JSON.parse(data.d);
  23.                     $.each(personList, function (id, person) {
  24.                         var personRow = "<tr><td>" + person.PersonID + "</td>" +
  25.                             "<td>" + person.Name + "</td>"+
  26.                             "<td>" + person.Registered + "</td></tr>";
  27.                         tablePerson.append(personRow);
  28.                     });
  29.             },
  30.             error: function (xhr, ajaxOptions, thrownError)
  31.             {
  32.                 alert('Failed to retrieve person list.');            
  33.             }
  34.         });
  35.     });
  36.     </script>
  37. </head>
  38. <body>
  39.     <form id="form1" runat="server">
  40.     <table id="tblPerson" border="1" style="border-collapse:collapse">
  41.         <tr>
  42.             <th>Id</th>
  43.             <th>Name</th>
  44.             <th>Registered</th>
  45.         </tr>
  46.     </table>
  47.     </form>
  48. </body>
  49. </html>
Now run the application and you will get the result as shown in Figure 1.1.



Figure 1.1 A List of Persons

Note that from the web form code you are concatenating a string for <td> tags to show person details. It is small markup section so it’s not looking complex but when you develop a complex markup then it will not be easy to maintain and read so you need to define a separate template for it and call it where it is needed on the web form. So let’s see the code for the template.

The following code snippet is for a Mustache template that will show a person's detail.
  1. <script type="text/template" id="tempPerson">
  2.     <tr>
  3.        <td>{{PersonID}}</td>
  4.        <td> {{Name}}</td>
  5.        <td>{{Registered}}</td>
  6.     </tr>
  7. </script>
The following code snippet renders the Mustache template above in JavaScript.
  1. function (id, person)
  2. {
  3.   var template = $('#tempPerson').html();
  4.   var html = Mustache.render(template, person);
  5. }
The following code snippet is for the web form that uses the Mustache template and renders it on the web. Your Person class and get Person list method will be the same as the pervious.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MustacheExample.aspx.cs"   
  2. Inherits="MustacheTemplate.MustacheExample" %>  
  3. <!DOCTYPE html>  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6.     <title></title>  
  7.     <script src="Scripts/jquery-2.1.0.min.js"></script>  
  8.     <script src="Scripts/mustache.js"></script>  
  9.     <script type="text/javascript">  
  10.         $(document).ready(function () {  
  11.             var tablePerson = $("#tblPerson");  
  12.             var divPerson = $("#divPerson");  
  13.             $.ajax({  
  14.                 cache: false,  
  15.                 type: "POST",  
  16.                 url: "ShowTemplate.aspx/GetAllPerson",  
  17.                 data: {},  
  18.                 contentType: "application/json; charset=utf-8",  
  19.                 dataType: "json",  
  20.                 success: function (data) {  
  21.                     var personList = JSON.parse(data.d);  
  22.                     $.each(personList, function (id, person)   
  23.                     {  
  24.                         var template = $('#tempPerson').html();  
  25.                         var html = Mustache.render(template, person);
  26.                         tablePerson.append(html);  
  27.                     });  
  28.                 },  
  29.                 error: function (xhr, ajaxOptions, thrownError) {  
  30.                     alert('Failed to retrieve person list.');  
  31.                 }  
  32.             });  
  33.         });  
  34.     </script>  
  35.     <script type="text/template" id="tempPerson">  
  36.         <tr>  
  37.             <td>{{PersonID}}</td>  
  38.             <td> {{Name}}</td>  
  39.             <td>{{Registered}}</td>  
  40.         </tr>  
  41.     </script>  
  42. </head>  
  43. <body>  
  44.     <form id="form1" runat="server">  
  45.     <table id="tblPerson" border="1" style="border-collapse:collapse">  
  46.         <tr>  
  47.             <th>Id</th>  
  48.             <th>Name</th>  
  49.             <th>Registered</th>  
  50.         </tr>  
  51.     </table>  
  52.     </form>  
  53. </body>  
  54. </html>   
Let’s run the application and you will get the same result as in Figure 1.1.

Now you want to show only registered users; the registered users that have the registered property as true. You then need to change your template as in the following:
  1. <script type="text/template" id=" tempPerson">  
  2.     {{#Registered}}  
  3.      <tr>  
  4.         <td>{{PersonID}}</td>  
  5.         <td> {{Name}}</td>  
  6.         <td>{{Registered}}</td>  
  7.      </tr>  
  8.     {{/Registered}}  
  9. </script> 
The code above returns only the two person's details that have the property Registered as true. You add your condition to the template without if and else; that is why it’s called “Logic-Less”.

Mustache.js with ASP.NET MVC

In the previous article “Rendering a Partial View and JSON Data Using AJAX in ASP.Net MVC” I introduced you to the concept of rendering JSON data in ASP.NET MVC where I used the string concatenation approach but this section of article will introduce you to how to use a Mustache client template in an ASP.NET MVC application instead of string concatenation. I will explain this concept with a simple example. The example is that books are showing in a web depending on publisher. I choose a publisher from a dropdown list then the books information is shown in the web page depending on publisher. So let’s see this example in detail.

Getting Started

I add the ADO.NET Entity Model to the application to do the database operations mapped from the “Development” database. The ADO.NET Entity Model is an Object Relational Mapping (ORM) that creates a higher abstract object model over ADO.NET components. This ADO.NET Entity Model is mapped to with “Development” database so the context class is “DevelopmentEntities” that inherits the DbContext class.

This “Development” database has two tables, one is the Publisher table and the other is the BOOK table. Tables have 1-to-many relationships, in other words one publisher can publish multiple books but each book is associated with one publisher. If you want to learn more about this application database design then please check my previous article “An MVC Application with LINQ to SQL”.

The ADO.NET Entity Model is mapped to both tables. The connection string for this has the same name as the context class name and this connection string is created in the web.config file. You can change the name of the connection string. The context class name and connection string name is just a convention, not a configuration, so you can change it with a meaningful name. The following Figure 1.2 shows the ADO.NET Entity Model mapping with both tables.



Figure 1.2 The ADO.NET Entity Model mapping with Publisher and Book tables

Now the ADO.NET Entity Model is ready for the application and it's time to move on the next step of the application, the model design so let’s see the application’s model.

Model Design

As you already know, the purpose of the application is to create two entities (Publisher and BOOK) that I have already created as the same, therefore I need to create two models, one for the publisher and the other for the books. I create a publisher model (Publisher.cs) under the Model folder that has a publisher id and publisher list as in the following code snippet.
  1. using System.Collections.Generic;  
  2. using System.ComponentModel.DataAnnotations;  
  3. using System.Web.Mvc;  
  4. namespace MustacheMVCApplication.Models  
  5. {  
  6.     public class PublisherModel  
  7.     {  
  8.         public PublisherModel()  
  9.         {  
  10.             PublisherList = new List<SelectListItem>();  
  11.         }  
  12.         [Display(Name = "Publisher")]  
  13.         public int Id { getset; }  
  14.         public IEnumerable<SelectListItem> PublisherList { getset; }  
  15.     }  
  16. } 
After the publisher model, I create a book model (Book.cs) in the same folder that has basic properties related to a book. The following is a code snippet for the book model.
  1. namespace MustacheMVCApplication.Models  
  2. {  
  3.     public class BookModel  
  4.     {  
  5.         public string Title { getset; }  
  6.         public string Author { getset; }  
  7.         public string Year { getset; }  
  8.         public decimal Price { getset; }  
  9.     }  
  10. } 
Now the models are ready for use so now to move on to the controller.

Controller Design

 I create two controllers, one for the publisher that shows a publisher’s list in a dropdown list and another is a book controller that shows book details depending on publisher. The publisher controller defines a single action method that is the same in both concepts; rendering a partial view and JSON data and the book controller also defines an action methods, that rendering JSON data.

Now create a publisher controller (PublisherController.cs) under the Controllers folder as per the MVC convention. This control has a single action method to show the publisher list on the view. The following is a code snippet for the publisher controller.
  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using System.Web.Mvc;  
  4. using MustacheMVCApplication.Models;  
  5. namespace MustacheMVCApplication.Controllers  
  6. {  
  7.     public class PublisherController : Controller  
  8.     {  
  9.         public ActionResult Index()  
  10.         {  
  11.             PublisherModel model = new PublisherModel();  
  12.             using (DAL.DevelopmentEntities context = new DAL.DevelopmentEntities())  
  13.             {  
  14.                 List<DAL.Publisher> PublisherList = context.Publishers.ToList();  
  15.                 model.PublisherList = PublisherList.Select(x =>  
  16.                                         new SelectListItem()  
  17.                                         {  
  18.                                             Text = x.Name,  
  19.                                             Value = x.Id.ToString()  
  20.                                         });  
  21.                                  }  
  22.             return View(model);  
  23.         }  
  24.     }  
  25. } 
Now create book a controller (BookController.cs) under the Controllers folder of the application. ASP.NET MVC offers native JSON support in the form of the JsonResult action result, that accepts a model object that it serialized into the JSON format. In order to add AJAX support to your controller actions via JSON, simply use the Controller.Json() method to create a new JsonResult containing the object to be serialized.

Now create an action method BooksByPublisherId() in the book controller that returns JsonResult. This action method retrieves a list of books depending on publisher id that passes as a parameter in this action method. The following is a code snippet for this book controller.
  1. using System.Collections.Generic;  
  2. using System.Linq;  
  3. using System.Web.Mvc;  
  4. using MustacheMVCApplication.Models;  
  5. namespace MustacheMVCApplication.Controllers  
  6. {  
  7.     public class BookController : Controller  
  8.     {  
  9.         public JsonResult BooksByPublisherId(int id)  
  10.         {  
  11.             IEnumerable<BookModel> modelList = new List<BookModel>();  
  12.             using (DAL.DevelopmentEntities context = new DAL.DevelopmentEntities())  
  13.             {  
  14.                 var books = context.BOOKs.Where(x => x.PublisherId == id).ToList();  
  15.                 modelList = books.Select(x =>  
  16.                                             new BookModel()  
  17.                                             {  
  18.                                                 Title = x.Title,  
  19.                                                 Author = x.Auther,  
  20.                                                 Year = x.Year,  
  21.                                                 Price = x.Price  
  22.                                             });  
  23.             }  
  24.             return Json(modelList,JsonRequestBehavior.AllowGet);   
  25.         }  
  26.     }  
  27. } 
I define a route for this action in the RegisterRoute() method under the RouteConfig class (App_Start/RouteConfig.cs).
  1. public static void RegisterRoutes(RouteCollection routes)  
  2. {  
  3.     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.     routes.MapRoute(  
  5.         name: "Default",  
  6.         url: "{controller}/{action}/{id}",  
  7.         defaults: new { controller = "Publisher", action = "Index", id = UrlParameter.Optional }  
  8.     );  
  9.     routes.MapRoute("BooksByPublisherId",  
  10.     "book/booksbypublisherid/",  
  11.     new { controller = "Book", action = "BooksByPublisherId" },  
  12.     new[] { "MustacheMVCApplication.Controllers" });  
  13. } 
Now create an Index view for the publisher with a dropdown list for the publisher and that shows the book details depending on the value selected in the publisher dropdown list using AJAX. This view uses a Mustache client-side template that shows book details. The following is a code snippet for a book detail.
  1. <script type="text/template" id="tempBook">  
  2.    <b>Title :</b> {{Title}} <br/>  
  3.    <b> Author :</b> {{Author}}<br/>  
  4.    <b> Year :</b> {{Year }} <br/>  
  5.    <b> Price :</b> {{Price }}<hr/>  
  6. </script> 
The following code snippet is for rendering this template in JavaScript.
  1. function (id, book)  
  2. {  
  3.     var template = $('#tempBook').html();  
  4.     var bookData = Mustache.render(template, book);  
  5. } 
The following is a code snippet for the entire Index view.
  1. @model MustacheMVCApplication.Models.PublisherModel  
  2. <script src="~/Scripts/jquery-2.1.0.min.js"></script>  
  3. <script src="~/Scripts/mustache.js"></script>      
  4. <script type="text/javascript">  
  5.     $(document).ready(function () {  
  6.         $("#Id").change(function () {  
  7.             var id = $("#Id").val();  
  8.             var booksDiv = $("#booksDiv");  
  9.             $.ajax({  
  10.                 cache: false,  
  11.                 type: "GET",  
  12.                 url: "@(Url.RouteUrl("BooksByPublisherId"))",  
  13.                 data: { "id": id },  
  14.                 success: function (data) {  
  15.                     var result = "";  
  16.                     booksDiv.html('');  
  17.                     $.each(data, function (id, book) {  
  18.                         var template = $('#tempBook').html();  
  19.                         var bookData = Mustache.render(template, book);  
  20.                         booksDiv.append(bookData);  
  21.                     });  
  22.                 },  
  23.                 error: function (xhr, AJAXOptions, thrownError) {  
  24.                     alert('Failed to retrieve books.');  
  25.                 }  
  26.             });  
  27.         });  
  28.     });  
  29. </script>  
  30. <script type="text/template" id="tempBook">  
  31.         <b>Title :</b> {{Title}} <br/>  
  32.         <b> Author :</b> {{Author}}<br/>  
  33.         <b> Year :</b>  {{Year }} <br/>  
  34.         <b> Price :</b> {{Price }}<hr/>  
  35.     </script>  
  36. <div>  
  37.     @Html.LabelFor(model=>model.Id)  
  38.     @Html.DropDownListFor(model => model.Id, Model.PublisherList)  
  39. </div>  
  40. <div id="booksDiv">  
  41. </div> 
Let’s run the application. You will get results as in Figure 1.3.



Figure 1.3 Books detail of a publisher

Conclusion

While the client template approach may seem like a lot of work, in most cases the ease of maintenance and the lower bandwidth costs that it allows make it well worth the up-front cost. When your application relies on many AJAX interactions that results in complex client-side markup, a client template is often a great choice.


Similar Articles