More Ext JS with ASP.NET MVC Goodies: Populating a GridPanel

Another simple example of how to use Ext JS with ASP.NET MVC. This time I am going to display a number of fictitious movies using an Ext JS GridPanel. The movies list will be served by an ASP.NET MVC application.

extjs-aspnet-mvc-sample-1

The View

As I explained in my previous Ext JS with ASP.NET MVC example, in a model-view-controller scenario the View renders entities from the Model into a form suitable for interaction.  

The View in this example is the html page containing the Movies grid.  The ExtJS components I use in this page are a JsonStore and a GridPanel:

Ext.onReady(function () {

    var filmsStore = new Ext.data.JsonStore({
        url: 'http://localhost/MvcApp/Films',
        root: 'Films',
        idProperty: 'Id',
        totalProperty: 'count',
        fields: ['Id', 'Title', 'ReleaseYear', 'Rating'],
        remoteSort: true,
        autoDestroy:true,
        autoLoad:true
    });

    var filmsGrid = new Ext.grid.GridPanel({
        title: 'Movies',
        store: filmsStore,
        columns: [
            { id: 'title-col', header: "Title", width: 180, dataIndex: 'Title', sortable: true },
            { header: "Rating", width: 65, dataIndex: 'Rating', sortable: true, align: 'right' },
            { header: "Year", width: 75, dataIndex: 'ReleaseYear', sortable: true, align: 'right' }
        ],
        autoExpandColumn: 'title-col',
        renderTo: Ext.getBody(),
        width: 400,
        height: 250,
        loadMask: true,
        columnLines: true
    });
            
});

The Controller

Requests to the /Films route in the view are sent to FilmsController.  This controller will ask the Model to produce the needed data.  When a request is made to the /Films route, the Index method will be executed:

public class FilmsController : Controller
{
    //
    // GET: /Films/

    public JsonResult Index()
    {
            
        FilmsContainer container = FilmsDataContext.GetFilms();

        return Json(container,JsonRequestBehavior.AllowGet);
    }

}

Note that Index returns Json-encoded data in a JsonResult instance.  This is the data the movies grid needs.

Building the Model

The model is the objects that represent the data and domain logic of the application.  A movie will be represented by an instance of the Movie Class:

public class Film
{
    public int Id { get; set; }
    public string Title { get; set; }
    public int ReleaseYear { get; set; }
    public string Rating { get; set; }
}

FilmsContainer is  a Class that exposes an array of Film objects.  An instance of this Class will be Json-serialized by the Index method of the FilmsController Class:

public class FilmsContainer
{
    public Film[] Films { get; set; }
    public int Count { get; set; }
}

FilmsDataContext creates the movies data. To keep the example simple, I am hard-coding a few films:

public class FilmsDataContext
{
    public static FilmsContainer GetFilms()
    {
        Film[] filmsArray = new Film[5];

        filmsArray[0] = new Film()
        {
            Id = 1,
            Title = "ACADEMY DINOSAUR",
            ReleaseYear = 2006,
            Rating = "PG"
        };
        filmsArray[1] = new Film()
        {
            Id = 1,
            Title = "ACE GOLDFINGER",
            ReleaseYear = 2006,
            Rating = "G"
        };
        filmsArray[2] = new Film()
        {
            Id = 1,
            Title = "AFFAIR PREJUDICE",
            ReleaseYear = 2006,
            Rating = "G"
        };

        filmsArray[3] = new Film()
        {
            Id = 1,
            Title = "AGENT TRUMAN",
            ReleaseYear = 2006,
            Rating = "PG"
        };
        filmsArray[4] = new Film()
        {
            Id = 1,
            Title = "ALICE FANTASIA",
            ReleaseYear = 2006,
            Rating = "NC-17"
        };

        FilmsContainer container = new FilmsContainer()
        {
            Films = filmsArray,
            Count = filmsArray.Length
        };
            
        return container;
    }
}

Downloads

Download the full sample from my downloads page.

Want to learn more?

Ext-JS-Cookbook My Ext JS 3.0 Cookbook has more than a hundred step-by-step recipes that you can use to build your Ext JS applications.  Download a sample chapter and see for yourself.

E-mail   Permalink    Comments(0)   Trackback

Tags: , ,

Teaching BlackBerry Development at Pratt & Whitney

I just had the opportunity to teach a BlackBerry development class at Pratt & Whitney. This class was about the techniques we use and the issues we face – as developers, architects or managers – when we bring mobile application development into our organizations.

Found in Wikipedia: Design Strategist

Please, don’t talk like this.

New Version of My Free GUI for the Microsoft Ajax Minifier

I just updated AjaxminGui, my free user interface for the Microsoft Ajax Minifier. Read more for the list of features.

The Making of QwikTime For iPhone

I just released the first version of QwikTime for iPhone and I want to tell you about what it took to get this product out the door in a short period of time. Read on.

Ben Rich on Efficiency and Optimization

Eighty percent efficiency would get the job done, so why strain resources and bust deadlines to achieve that extra 20 percent...

BlackBerry Development Class

I will start teaching a BlackBerry development class. This class will consist of a number of hands-on labs where you will build an entire real-world BlackBerry application from scratch.

How to Save BlackBerry Settings in the Persistent Store

In this article we will create a generic Class that we can use in our BlackBerry Java applications to save and retrieve settings and user preferences from the Persistent Store.

Using Backgrounds and Borders in BlackBerry Super Apps

In this article you will learn how to change the background and borders of UI fields in BlackBerry applications.

Recommended Books for Any Software Developer

I just created a list of some of the books I have read that I would recommend to any software developer. I hope you find it helpful.

Ext JS Data Store Dependency Injection

Here's an explanation of a feature that gives components that use data stores the ability to reference such stores by their unique identifiers.

Ext JS Charts: Creating line charts without markers

In this article we will look at a simple configuration we can use with the Ext JS line charts in order not to display the chart markers.

Ext JS with PHP: How to Add Children to Async Tree Nodes

This article will show you how to use PHP to add child nodes to any async node of an Ext JS TreePanel

Using the Ext Scheduler, Part 2

This is another article where we explore the Ext JS Scheduler component while we use it to build a hypothetical conference room reservations interface.

Ext JS with PHP: How to Create Nodes for a TreePanel

This article will show you how to use PHP to create the nodes of an Ext JS TreePanel