When I first started building liteGrid, one of my design goals was to build a grid library that worked easily with ASP.NET MVC.   I’m not sure I’ve delivered on that goal, at least not externally.  Internally, we’ve built quite a lot of MVC plumbing that makes working with liteGrid easier.  We have custom JSON result types, view models, helpers, etc.  Sadly time has not yet permitted me to migrate most of these improvements back into the (empty) Visual Studio .NET project that’s in liteGrid, but I plan to start doing that as soon as I have time to do some cleanup and refactoring.  In the meantime, I thought I would start showing off some of the plumbing that we’ve built, starting with the LiteGridJsonResult.

The LiteGridJsonResult is used to send data to an instance of liteGrid.  The intended use is something like this:

//Get your objects from wherever
var domainObjects = GetDomainObjects();

return new LiteGridJsonResult(
    domainObjects.Select(d => new MyCustomViewModel(d)).ToArray()
);

Note that we’re converting our domain objects to view models, then sending the view models to liteGrid through the result.  Client-side, this means that liteGrid is going to receive data that looks exactly like the view models we’re working with server-side.  This allows us to build our column definitions, validation rules, etc. from our view models in a strongly-typed way. 

The actual implementation looks like this:

/// <summary>
/// A custom JsonResult that returns data in a format that 
/// liteGrid expects.
/// </summary>
public class LiteGridJsonResult : StandardJsonResult
{
    /// <summary>
    /// The data items being sent to the grid.
    /// </summary>
    public Array DataItems { get; private set; }

    /// <summary>
    /// Creates a result with the specified data items. 
    /// </summary>
    /// <param name="dataItems"></param>
    public LiteGridJsonResult(Array dataItems)
    {
        DataItems = dataItems;
    }

    /// <summary>
    /// Copies the values into the Data anonymous object. 
    /// </summary>
    /// <param name="context"></param>
    public override void ExecuteResult(ControllerContext context)
    {
        //The base class serializes the DataItems to JSON
        //and assigns it to a 'dataItems' property in the result.
        Properties.Add("dataItems", DataItems);

        base.ExecuteResult(context);
    }
}

The base class, StandardJsonResult, is a specialization of MVC’s JsonResult that we’ve created.  It exposes a Boolean Status property that indicates success/failure of a JSON operation in a consistent way (poor choice of name in hindsight, but it serves the intended purpose).  It was originally created when we were using anonymous types instead of view models, and was intended to make it easier to test that objects were being projected correctly (yeah, I know, bad idea).  It’s on my TODO list to rework it.  For now, suffice to say that it takes care of projecting our view models into JSON that looks like this (the format that liteGrid expects):

{
 status: true,
 dataItems: [{ID: 1, Name: "Name", Title: "Title"}, {ID: 2, Name: "Name", Title: "Title"}]
}

LiteGridJsonResult is still a little rough around the edges (this blog post actually reminded me of how much work we still need to do), but it works.  In the next post, I’ll show you how we handle synchronizing changes from liteGrid back to the server-side object model.