New Ajax Grid Scaffolding NuGet Package for MVC 3

EDITOR’S NOTE: Microsoft has an amazing intern program. For a summer, these bright college students work with a feature crew getting real work done, all the while attending cool events nearly every week that, frankly, make the rest of us jealous! Just look at some of the perks listed in this news article!

This summer, the ASP.NET MVC is hosting an intern, Stephen Halter, who while very smart, doesn’t have a blog of his own (booo! hiss!). Being the nice guy that I am (and also being amenable to bribes), I’m letting him guest author a post on my blog (first guest post ever!) to talk about the cool project he’s been doing.

Editor Update: A lot of folks are wondering why we built our own grid. The plan is to build this on top of the jQuery UI Grid when it’s complete. It’s not done yet so we built a scaled down implementation to allow us to test out the interactions with ASP.NET MVC controllers and make sure everything is in place. It’s mostly T4 files that extends our existing Add Controller Scaffolding feature. You can tweak the T4 to build any kind of grid you want.

Hello everyone – my name is Stephen Halter, and I am a summer intern on the ASP.NET MVC team who is excited to show you the new Ajax grid scaffolder that I have been working on for ASP.NET MVC 4. Phil has graciously allowed me to use his blog to get the word out about my project.

We want to get some feedback before releasing the scaffolder as part of the MVC Framework, and we figured what better way to get feedback than to release a preview of the scaffolder as a NuGet package?

To install the package, search for “AjaxGridScaffolder” in the NuGet Package Manager dialog or just enter the following command

PM> Install-Package AjaxGridScaffolder

in the Package Manager Console to try it out.

The NuGet package, once installed, registers itself to the Add Controller dialog as a ScaffolderProvider named “Ajax Grid Controller.” When run, the scaffolder generates a controller and corresponding views for an Ajax grid representation of the selected model and data context class.

The Ajax grid controller scaffolder has many similarities to the current “Controller with read/write actions and views, using Entity Framework”, but it provides pagination, reordering and inline editing of the content using Ajax.

Add > Controller menu selection

Template Selection - Ajax Grid Controller

Currently, the Ajax Grid Scaffolder only generates C# code, but both Razor and ASPX views are supported. We’ll add VB.NET support later. If you selected “None” from the Views drop down in the Add Controller dialog, only the controller will be generated.

The generated controller has 8 actions. There is Index, Create (POST/GET), Edit (POST/GET), Delete (POST), GridData, and RowData. Most of these actions probably sound familiar, but there are a few that might not be obvious. The GET versions of Create and Edit provide editing widgets to the client using HtmlHelper.EditorFor or HtmlHelper.DropDownList if the property is a foreign key. The GridData action renders a partial view of a range of rows while RowData renders a partial view a specific row given its primary key. These *Data actions aren’t very verby (editor’s note: “verby?” Seriously?) and improved naming suggestions are welcome.

Solution explorer view of the project structure

The scaffolder also provides three views: Index, GridData, and Edit. The Index view includes JavaScript and small amount of CSS required to make the Ajax grid usable. Due to all the possible ways a master or layout page can be done, it isn’t possible to ensure that JavaScript and CSS will be included in the <head> element of the document. However, we are looking at possible fixes for the MVC 4 release.

clip_image005

The GridData and Edit views are partial views that are rendered when retrieving non-editable and editable rows respectively via XMLHttpRequest instances (XHRs) using jQuery. Sending HTML snippets in response to XHRs (as opposed to JSON for example) makes using DisplayFor, EditorFor and ValidationFor more natural and progressive enhancement simpler.

With or without JavaScript, the Index view provides pagination and reordering by column. With JavaScript enabled, rows can be created/edited/deleted all inside the grid without ever leaving the index view.

Tell us what your thoughts are on the Ajax Grid Scaffolder. Is it important to you that we support editing without JavaScript? Do you want the back button to take you to your previous view of the grid? Do you have any suggestions on how to clean up the generated code? Did you encounter any bugs/issues? If so, we want to know.

What others have said

Requesting Gravatar... Jayesh Dhobi Aug 17, 2011 2:13 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
This is very good update in MVC3. It is useful to developer to make fast development.
Requesting Gravatar... Kazi Manzur Rashid Aug 17, 2011 2:28 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Looks neat, the first thing MS should address the proper use of Http verbs and status codes. No more POST for update, instead use PUT, same goes to delete, 201 when a resource is created, currently none of the action result takes extra http status codes. Add some kind of helper methods which adds the CRUD routes in route table. example:

/products - GET - returns list of products
/products - POST - creates new product
/products/{id} - PUT - updates existing product
/products/{id} - DELETE - deletes product

The default controller which the scaffolder generates does not at all friendly when you use framework like backbone/spine.
Requesting Gravatar... KKBruce Aug 17, 2011 2:52 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Waiting VB. Thanks.
Requesting Gravatar... Carsten Petersen Aug 17, 2011 3:41 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Finally some great grid scaffolding =) But it seams like there is a bug when clicking Edit. The GET request uses parameters which isn't valid (imo).

Controller Action:
Edit(string id)

Request Parameter (in Firebug):
__ID__?_=XXXXXXXX

(My entity key is named "ID")
Requesting Gravatar... Remy Aug 17, 2011 4:25 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
You say this also supports ASPX views. You mean Webforms? Do you have an example on how to use it with Webforms?
Requesting Gravatar... Michiel Post Aug 17, 2011 5:03 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Tried it out and it's cool!

- Can the Create/Save/Cancel get a fixed width? If you have a row with only two columns, these buttons get really big.
- Sorting, maybe just one arrow instead of two. Only show the arrow that displays the current sort order. It makes the table header less cluttered.
- Add a scaffolding option that also generates regular create/edit pages. I don't always want to show and edit all the data in the grid. Most of the time I only show the important columns. Less regular data can be edited through a regular edit page.

Thanks for the great NuGet package!
Requesting Gravatar... Andrew Wrigley Aug 17, 2011 6:05 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Great effort, but...

...Should it be an intern developing these things? Should this kind of scaffolding not be a central part of ASP.NET MVC updates?

Eg, Menus, Wizards, etc, etc.
Requesting Gravatar... Al Aug 17, 2011 6:17 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Make the controller RESTful to make interaction with other stuff easy and you are on to a winner. If you added the option of a choice between returning JSON or PartialViews it would be very helpful. Even if we just had a scaffolder to implement RESTful json controller/repositories it would be extremely useful and encourage best practice!
Requesting Gravatar... Justin Aug 17, 2011 6:21 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
@Andrew,
I agree. There needs to be a stock datagrid as part of the mvc core although the grid from mvccontrib is pretty nice. Why was this not done by the mvc team early on?
Requesting Gravatar... Scott Galloway Aug 17, 2011 6:43 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Really nice...(and really Phil, you commenting on proper english usage ;)). I do wonder if you're not duplicating work already done on stuff like jqGrid...it has way more functionality (obviously being an older product), would it not be worthwhile exploiting the investment MVC already made in jQuery and expanding that to some of the preexisting controls?
Oh, and for the guy commenting on interns...ASP.NET interns ain't 'normal' interns...as a general rule they're 99th+ percentile devs :)
Requesting Gravatar... Simon Bartlett Aug 17, 2011 8:38 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
@Scott Galloway I get your point about jqGrid, but IMO it's heavy, clunky, and just plain ugly. If only the jQuery UI Grid was ready, huh?

To those who are commenting about interns; I'm sure DevDiv would not just hand a project over to be solely worked on by an intern. I'm sure the intern will be working in a team, and having to follow the same quality processes as the rest of the team.

A good intern program is one where adequate attention is given to the intern and what they're doing; not one in which the intern is given a task to complete by themselves and then abandoned. I mean the whole point of an intern program is to get real-world experience, and in DevDiv that will involve being a part of a product team, following processes including code reviews and design phases.
Requesting Gravatar... Abox Aug 17, 2011 9:31 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
it doesnt propose me any scaffolding... do i miss something?
Requesting Gravatar... Sam Aug 17, 2011 9:37 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
I like what you've done here. It mimics a fair amount of my own personal real-world data-grid-like MVC creations. However, I don't really think that this needs to be part of the MVC core. Doing that makes me think web forms (drag-n-drop) data grids, etc. (yuck!). I guess it's a fine and dandy thing to add to a scaffolding nuget package and include with a project template, I'm just weary about this giving way to "demo-able" features that are not very "real-world usable".
Requesting Gravatar... Craig Stuntz Aug 17, 2011 9:49 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Thanks for asking for feedback.

Yes, back button support is important. It's also not hard. I got it working for jqGrid, which didn't support it; see my GitHub fork here:

https://github.com/CraigStuntz/jqGrid/wiki

Regarding adding this as an MVC *framework* feature, I think if you limit yourself to one grid you've already lost. As someone pointed out above, the jQuery UI grid is coming out RSN.

However, it's possible to make an interface which supports multiple grids. I have a prototype of such a thing here:

http://anygrid.codeplex.com/

These are all very solvable problems.
Requesting Gravatar... David Ruttka Aug 17, 2011 9:53 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Index is a noun in this context, so I wouldn't worry too much about "verbiness". Perhaps GridData can become Rows, and RowData can become Row. If you still want alternatives, perhaps consider something like Set and Single, or Rows and Row.
Requesting Gravatar... David Ruttka Aug 17, 2011 9:55 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Yeah, I repeated myself in that last comment. Still waking up. :)
Requesting Gravatar... Ben Aug 17, 2011 11:12 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
I was looking for something to quickly toss together some data admin tools for a site I've been working on. This looks perfect. Great post.
Requesting Gravatar... magellin Aug 17, 2011 11:26 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Pretty sweet. I'd like to see what we can do for something like DataTables or the upcoming jQuery UI grid in the future.
Requesting Gravatar... Thiago Custodio Aug 18, 2011 1:32 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
How to do that if I'm using Dependency Injection to persist my entities? I won't have the data context class, this will be handled by my service layer that I'll inject on my controller.
Requesting Gravatar... haacked Aug 18, 2011 2:37 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Hi all, thanks for the comments.

@Scott Galloway: The plan for this grid is to build it on the jQuery UI Grid. However, that grid is not yet complete, so we built a simpler one to test out the interactions with the server.

Once that's released, we'll rebuild on that one.

@Kazi we can look into that. One problem today using PUT is that it doesn't work by default in some IIS configurations and not many folks realize it. So if we did that by default, you deploy your application, what worked locally might not work in your deployed server.

@Craig This grid just hooks into the existing Controller Scaffolder extensibility built into ASP.NET MVC's Add Controller dialog. It's really just a collection of T4 files with some glue code.

So anyone can take what we did, tweak the T4 files, and scaffold onto any Ajax grid implementation.

@Andrew We only hire the smartest interns. And the intern works with a mentor. Also, the feature team reviews the code and will take ownership of the code when the summer is over.
Requesting Gravatar... Stephen Halter Aug 18, 2011 4:13 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
@Carsten

Do the &lt;tr&gt;s genertated by the GridData partial view contain a data-pkey attribute? If not, the scaffolder either found more than one column in the primary key (something which I hope we will support in the future) or didn't find a primary key at all.
Requesting Gravatar... Gabriel Rodriguez Aug 18, 2011 4:50 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
@Remy no, he means the ASPX ViewEngine (with .aspx file extension). Razor uses .cshtml.
Requesting Gravatar... Lavisnki Aug 18, 2011 12:28 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
I would like to see json supported as well as html for the grid data. Paging through data is something I do only if a free scrolling approach is not applicable.
Requesting Gravatar... Scott Galloway Aug 21, 2011 8:53 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Ah, excellent, knew you'd be on the ball here. (Fingers crossed about jQuery UI Grid, some of the existing jQuery UI stuff is a little messy)
Requesting Gravatar... Jessica Sep 04, 2011 9:30 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
It seems you're not generating properly escaped SQL. I got an EntitySqlException: 'set' is a reserved keyword and cannot be used as an alias, unless it is escaped.

At least the Entity Framework seems able to catch your security holes...
Requesting Gravatar... fitz Sep 13, 2011 7:03 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
ok - so I used this a few times - one time it was fine -

The last time I used it the Edit/Delete buttons never come up - how did that happen - the columns for those are stuffed in some 'hidden' class - what gives?

:/

Requesting Gravatar... Dave Sep 19, 2011 6:16 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Hi,

I installed the package and gave it a whirl with an EF4.1 (database first) model annotated using metadata classes.

The package fails reporting that it is unable to retrieve metadata for the object and that the supplied connection is not valid because it contains insufficient mapping or metadata information.

Suggestions?
Requesting Gravatar... Tone Oct 27, 2011 9:45 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Vb, vb, vb, vb...

Is c# the new script kiddie language (in that anyone can do it)? My winder for today ;)

That should be my sig but just wondering if any progress.

Looks like good work from what I see here but don't want to nuget to C# until. I have a project to use it with.

Don't forget grouping is all I have to say :)
Requesting Gravatar... sunil Nov 26, 2011 5:03 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Do I need jqgrid installed to use this. Getting an error when creating controller code.
Modified config file for remote loading but still showing same error. Any help appreciated.
Requesting Gravatar... BhaktaVatsal Dec 14, 2011 10:49 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
We need an excellent grid within an MVC that can beat the control vendors :-) of course they won't be happy but making it native to any offering is what makes it kind of better choice for enterprises, since no one wants to bind themselves to control vendors these days.

nice work, and will be waiting for the grid release that can be used heavily in financial industries in MVC 4.
Requesting Gravatar... Benjamin Jan 19, 2012 6:40 AM
# automation
Can this be executed/scaffolded from the Package Manager console?
Requesting Gravatar... Parihar Feb 03, 2012 6:20 AM
# re: how to get column value of a perticular row from webgrid in mvc3 framework using javascript
Its a webgrid problem in mvc3 from my side.

I made a shopping cart application. I want to get details of selected product from a product table in webgrid.

Can anyone help me to find the column value of a selected row in webgrid using javascript and send this to another page using session.

Requesting Gravatar... afro Feb 14, 2012 1:53 PM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
Hi,
I'm trying to find T4 templates
1). ListViews like these the only exception is that I want Add/Edit views in popup.
2). Master Details or ListViews with tabcontrols every tab having the Add/Edit based on webgrid not jqGrid
3). Popup selections

is there anyone who has any idea about it, thanks

Requesting Gravatar... Jim Feb 15, 2012 8:11 AM
# re: New Ajax Grid Scaffolding NuGet Package for MVC 3
yes history is important, but also what about filtering?

What do you have to say?

(will show your gravatar)
Please add 6 and 5 and type the answer here: