Please check out my previous posts (Part1 and Part2) if you haven’t read it yet.

Today I want to show how we can create a generic “GridBaseController for our jqTGrid that we don’t have to implement the “DynamicGridData” controller method in every controller.

For that reason I created a new generic class called “GridBaseController”:

public class GridBaseController<T> : Controller where T : class,new () {
  [HttpPost]
  public ActionResult DynamicGridData(string sidx, string sord, int page,
                                      int rows) {
    var context = new GridEntities<T>();
    return (context.GenericType.ToList().AsQueryable().AsJqGridResult(sidx, sord,
                                                                      page, rows));
  }
}

Here you can see that I changed the “HaackOverflowEntities” to “GridEntities”. This class extends the existing “HaackOverflowEntities” and holds a GenericType which creates an “ObjectSet
based on the passing data object.

  public class GridEntities<T> : HaackOverflowEntities where T : class,new (){
    /// <summary>
    /// Gets the objectset for the generic data type
    /// </summary>
    public ObjectSet<T> GenericType {
      get {
        if ((_genericType == null)) {
          _genericType = base.CreateObjectSet<T>();
        }

        return _genericType;
      }
    }
    private ObjectSet<T> _genericType;
  }

The only thing we have to do now is inherit our controllers from the ”GridBaseController”.

public class SimpleController : GridBaseController<Question> {         
  //`
  // GET: /Simple/           
  public ActionResult Index() {                   
    return View();
  }
}

I think with the use of the “GridBaseController” we can create now very fast and simple jqGrids.

Here is the new source code if you are interested.