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: , ,

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

Writing an Ext JS Plugin

In this article I will show you how to write an Ext JS plugin that displays how many characters you can type on a textarea before a maximum number of characters is reached.

What Is An ExtJS Plugin?

Here are some facts that will help you understand Ext JS plugins.

Using the Ext Scheduler, Part 1

A review of the ext scheduler component

Ext JS Tips: Binding an Array of Objects to a GridPanel

How to bind an array of objects to an Ext JS GridPanel

Learn To Customize the Ext JS Charts

How to change an Ext JS Chart's data series style

Ext JS Books Round-up

Here is a list of the Ext JS books currently in the market.

Custom Markers for Your Ext JS Charts

How to customize your Ext JS charts by changing the look of the data point markers.

Display an Image Inside an Ext JS GridPanel’s Cell, Part 3

How to position an image inside an Ext GridPanel cell using a template column.

Displaying an Image Inside an Ext JS GridPanel Cell, Part 2

This is how you can display an image and a value inside an Ext GridPanel cell using a renderer function.