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.
So, given the support for DataAnnotations in MVC2, how do you see this fitting into validation on the grid itself?
We actually have solid validation support via attributes thanks to work James has done. The approach is very similar to xVal: you decorate your view models with DataAnnotations, call a helper in the view, and the appropriate validation rules are applied to the grid. I believe the latest liteGrid demo actually shows this capability.
Do you have any update for the lucene.net asp.net mvc faceted search? (following
http://trycatchfail.com/blog/post/2009/05/01/Hierarchical-Faceting-for-LuceneNET-Part-1.aspx)
Sorry for placing this comment in the wrong place, but didn’ find any other way to contact you.
Thank you!
Hi webmaster, your website’s theme is pretty cool and . Your posts are totally great. Please continue posting. Greets!..
I really loved this awesome blog post. Please keep up the good work. Regards, Sammy!!
Considerably, the post is really the best on this deserving topic. I concur with your conclusions and will eagerly look forward to your approaching updates. Just saying thanks will not just be enough, for the extraordinary lucidity in your writing. I will right away grab your rss feed to stay privy of any updates. Pleasant work and much success in your business endeavors!
So not really on the same topic as your post, but I found this today and I just can’t resist sharing. Mrs. Agathe’s dishwasher quit working so she called a repairman. Since she had to go to work the next day, she told him, “I’ll leave the key under the mat. Fix the dishwasher, leave the bill on the counter, and I’ll mail you the check. Oh, and by the way…don’t worry about my Doberman. He won’t bother you. But, whatever you do, do NOT under ANY circumstances talk to my parrot!” When the repairman arrived at Mrs. Agathe’s apartment the next day, he discovered the biggest and meanest looking Doberman he had ever seen. But just as she had said, the dog simply laid there on the carpet, watching the repairman go about his business. However, the whole time the parrot drove him nuts with his incessant cursing, yelling and name-calling. Finally the repairman couldn’t contain himself any longer and yelled, “Shut up, you stupid ugly bird!” To which the parrot replied, “Get him, Spike!”
Hi everyone, This webpage is good quality and so is the way the topic was written about. I like some of the comments as well even though I would suggest we all stay on the suject in order add value to the idea. It will be also encouraging to the one who penned it down if we all could make mention of it (for those who use social media such as a reddit, twitter,..). Thanks again.
Hello. This is kind of an "unconventional" question , but have other visitors asked you how get the menu bar to look like you’ve got it? I also have a blog and am really looking to alter around the theme, however am scared to death to mess with it for fear of the search engines punishing me. I am very new to all of this …so i am just not positive exactly how to try to to it all yet. I’ll just keep working on it one day at a time Thanks for any help you can offer here
I truly liked this brilliant article. Please keep up the good work. Regards from Samantha.
<P class=MsoNormal style="MARGIN: 0in 0in 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: ‘Arial’,’sans-serif’">I love this….lol<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p></SPAN></P>
Getting out of credit card debt can be really tough. Here are 3 tips <a href="http://ezinearticles.com/?id=3681539">on how to get out of credit card debt</a>
Great insights. I love to read your articles. Always interesting topics, and excellent research. You must be putting a lot of time into your blog.