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

jQuery Templates/View Engines in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
4.88/5 (61 votes)
25 Apr 2012CPOL22 min read 215.5K   4.1K   179  
This article explains how client-side view engines can be used in ASP.NET MVC.
This article introduces a different concept of the view engine - a client-side view engine that renders a view in a client browser. In this concept, the model and the controller are still on the server-side. However, instead of the HTML, JSON is generated as an output from the server-side, it is accepted on the client-side, and HTML is generated using the JavaScript templating engine.
using System;
using System.Collections.Generic;

namespace LoadJSON.Models
{
    public class DataRepository
    {

        public static IList<Company> CompanyData = null;
        public static IList<Company> GetCompanies()
        {
            if (CompanyData == null)
            {
                CompanyData = new List<Company>();
                CompanyData.Add(new Company()
                {
                    Name = "Emkay Entertainments",
                    Address = "Nobel House, Regent Centre",
                    Town = "Lothian",
                    Contact = "Phone",
                    IsFeatured = true,
                    Logo = "/Content/images/logo17.jpg",
                    Country = new[] { "UK", "USA" },
                    Manager = new Employee { FirstName = "Nick", LastName = "Donovan" },
                    Employees = new Employee[] { new Employee { FirstName = "John", LastName = "Doe" },
                                                                             new Employee { FirstName = "Alex", LastName = "Watkins" },
                                                                             new Employee { FirstName = "Mick", LastName = "Henning" }
                                                }
                });
                CompanyData.Add(new Company()
                {
                    Name = "The Empire",
                    Address = "Milton Keynes Leisure Plaza",
                    Town = "Buckinghamshire",
                    Contact = "Post",
                    Logo = "/Content/images/logo18.jpg$Logo for company with id 18",
                    IsFeatured = false,
                    Manager = new Employee { FirstName = "Ana", LastName = "Frank" },
                    Employees = new Employee[] {    new Employee { FirstName = "Mark", LastName = "Anderson" },
                                                                                new Employee { FirstName = "John", LastName = "Haus" },
                                                                                new Employee { FirstName = "Sinthia", LastName = "Clouny" } }
                });
                CompanyData.Add(new Company()
                {
                    Name = "Asadul Ltd",
                    Address = "Hophouse",
                    Town = "Essex",
                    Contact = "Email",
                    Logo = "/Content/images/logo19.jpg$Logo for company 19",
                    Country = new[] { "UK" },
                    Manager = new Employee { FirstName = "John", LastName = "Feigan" },
                    Employees = new Employee[] { new Employee { FirstName = "Micheal", LastName = "King" },
                                                                             new Employee { FirstName = "Jody", LastName = "Palmer" },}
                });
                CompanyData.Add(new Company()
                {
                    Name = "Star Records",
                    Contact = "Email",
                    Logo = "/Content/images/logo20.jpg",
                    Country = new[] { "SRB" },
                    Manager = new Employee { FirstName = "Steve", LastName = "Carter" },
                    Employees = new Employee[] { new Employee { FirstName = "Andrea", LastName = "Smith" }, 
                                                                             new Employee { FirstName = "Jane", LastName = "Anderson" } ,
                                                                             new Employee { FirstName = "Anthony", LastName = "Willis" },
                                                                             new Employee { FirstName = "George", LastName = "Hill" },
                }
                });
                CompanyData.Add(new Company()
                {
                    Name = "Ashley Mark Publishing Company",
                    Address = "1-2 Vance Court",
                    Town = "Tyne & Wear",
                    IsFeatured = true,
                    Country = null,
                    Manager = new Employee { FirstName = "Micheal", LastName = "Johnson" },
                    Employees = new Employee[] { new Employee { FirstName = "Micheal", LastName = "Sharp" },
                                                                             new Employee { FirstName = "Jeff", LastName = "Cave" }, 
                                                                             new Employee { FirstName = "Peter", LastName = "Hayward" },
                                                                            }
                }); 
            }
            return CompanyData;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Program Manager Microsoft
Serbia Serbia
Graduated from Faculty of Electrical Engineering, Department of Computer Techniques and Informatics, University of Belgrade, Serbia.
Currently working in Microsoft as Program Manager on SQL Server product.
Member of JQuery community - created few popular plugins (four popular JQuery DataTables add-ins and loadJSON template engine).
Interests: Web and databases, Software engineering process(estimation and standardization), mobile and business intelligence platforms.

Comments and Discussions