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.

Share and Enjoy:
  • Technorati
  • Digg
  • Facebook
  • del.icio.us
  • Live
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • TwitThis
  • Blogosphere News
  • Blogplay
  • LinkedIn
  • MisterWong
  • MisterWong.DE
  • MSN Reporter
  • MyShare
  • RSS
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Tumblr
  • Twitter
  • Webnews.de
  • Yahoo! Bookmarks
  • Yigg

In my last post I was talking about my idea to use the jqGrid to create a grid based on the data model. In this post I want to take the existing jqTGrid and go one step further. – What if we want to hide a column? What about the column header text?

The first step what I did for that was to change Linq2Sql to the Entity Framework.

That is actually not a big deal and I was surprised that everything worked so straight but when I saw the page I figured out that there are new columns.

image
(weiterlesen…)

Share and Enjoy:
  • Technorati
  • Digg
  • Facebook
  • del.icio.us
  • Live
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • TwitThis
  • Blogosphere News
  • Blogplay
  • LinkedIn
  • MisterWong
  • MisterWong.DE
  • MSN Reporter
  • MyShare
  • RSS
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Tumblr
  • Twitter
  • Webnews.de
  • Yahoo! Bookmarks
  • Yigg

If you searching the internet for jqGrid and ASP.NET MVC you will find many examples,
but all of them always define the jqGrid columns – see for an example the blog post from Phil Haack.
If you have a lot of jqGrids in your project you don’t want to define every single column for all the grids.

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery("#list").jqGrid({

            url: '/Home/GridData/',
            datatype: 'json',
            mtype: 'GET',

            colNames: ['Id', 'Votes', 'Title'],
            colModel: [
          { name: 'Id', index: 'Id', width: 40, align: 'left' },

          { name: 'Votes', index: 'Votes', width: 40, align: 'left' },

          { name: 'Title', index: 'Title', width: 200, align: 'left'}],

            pager: jQuery('#pager'),
            rowNum: 10,
            rowList: [5, 10, 20, 50],

            sortname: 'Id',
            sortorder: "desc",
            viewrecords: true,

            imgpath: '/scripts/themes/coffee/images',
            caption: 'My first grid'
        });
    });

</script>

So I came up with the idea, why not just pass the data model class and let the grid create itself based
on the model!
(weiterlesen…)

Share and Enjoy:
  • Technorati
  • Digg
  • Facebook
  • del.icio.us
  • Live
  • Google Bookmarks
  • DotNetKicks
  • DZone
  • TwitThis
  • Blogosphere News
  • Blogplay
  • LinkedIn
  • MisterWong
  • MisterWong.DE
  • MSN Reporter
  • MyShare
  • RSS
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Tumblr
  • Twitter
  • Webnews.de
  • Yahoo! Bookmarks
  • Yigg