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.
[...] This post was mentioned on Twitter by Hire ASP.Net Experts. Hire ASP.Net Experts said: News from LieberLieberBlog: ASP.NET MVC and a Generic jqQuery Grid –jqTGrid Part 3 http://bit.ly/d3MJgc [...]
[...] Make next idea is now to have a base controller which implements already the controller method for the grid so that you only would need to place the grid on the page! – You can find that here. [...]
[...] to VoteASP.NET MVC and a Generic jqQuery Grid –jqTGrid Part 3 (7/9/2010)Friday, July 09, 2010 from Simon GorskiPlease check out my previous posts ( Part1 and Part2) if you [...]
The sample code doesn’t work, in either part (1, 2, 3). The problem seems to be that there is a circular code dependency and that no code seems to be initiating the rendering.
If we start in Grid.Render and work our way backwards we get the following chain:
Render() is called by ToString() is called by BuildJqGrid() is called by RenderJqGridJavaScript is called by Render() again.
How to solve this?
Hi Linus,
what kind of exception do you get? The uploaded code is working fine for me.
Please make sure that the database connection string is correct!
Otherwise please post the exception details.
Thanks
Simon
Hi Simon,
Thank you very much for the reply.
Well, the symptom is not that I get an exception, but that the grid doesn’t get rendered at all. However, it might be a browser specific problem in combination with my customer’s (I’m an IT consultant) browser policy.
I actually get the sample code to work with IE and Chrome, but not in Firefox (which I’ve been using thus far when developing). Unfortunately I can’t verify if the code works with Firefox on other machines that doesn’t have a browser policy that my customer has (I only have Visual Studio here at my customer).
Basically, the policy is that I need to go through a proxy to be able to surf the web with Firefox, but I haven’t gotten it to work so I can’t surf with Firefox. I find it wierd that Firefox wouldn’t render the table at all though. It should at least render the headers and foooters even if it doesn’t reach the database.
Oh well, using Explorer or Chrome will solve my problem for the time being.
Thanks.
Sincerely,
Linus