Posts Tagged ‘MVC’

ASP.NET MVC 1.0 is finally out.  Download it now!


http://www.microsoft.com/downloads/details.aspx?FamilyID=53289097-73ce-43bf-b6a6-35e00103cb4b&displaylang=en

At first glance, upgrading to the RC1 was quite a debacle. First of all, any link that I created using the much more performant Html.RouteLink (instead of Html.ActionLink) just stopped working. It turns out there was a regression bug in the RC that caused this. The ASP.NET MVC team quickly released a refresh which fixed the problem.

The next problem came when I noticed Stephen Walther’s paging helper stopped working. It would throw a null reference exception whenever I used the Html.Pager() helper method. The issue was a breaking change in the RC1 with the location the RequestContext.

To fix the issue, you can download the solution from Stephen’s site. Open up the solution and change all the references to the latest ASP.NET MVC assemblies. When you compile the main project, you’ll find an error in the PagerBuilder.cs class at line 165. All you have to do is update this line to:

var urlHelper = new UrlHelper(_helper.ViewContext.RequestContext);

Compile the project and replace the assembly in your ASP.NET MVC project. I’m sure Stephen will update the project eventually so you could just download the latest version.

Hope this helps!

First of all, an apology for being so quiet over the past few weeks.  I’ve been putting my blogging aside for a while to chase some pet projects I’m working on.  More on that later.

I did, however, run into a particular situation that I thought would be of interest to anyone adopting ASP.NET MVC and looking for some goodies from jQuery.  Many of you have no doubt started testing the waters with jQuery since it was announced that Microsoft will be shipping the next version of .NET with it included.  If you haven’t used jQuery yet, prepare to fall in love!  

In this post, I’m going to cover a simple way to build a deep, hierarchical and feature-rich tree structure with only one line of jQuery script.  jQuery has a huge library of plugins covering all sorts of functionality.  For this, we’ll be using a neat plugin from for treeviews.  Here’s what you need:
jQuery
jQuery Treeview Plugin

This structure will consist of a category/sub-category relationship that is able to support n-levels of sub-categories with no changes.  To demonstrate, let’s look at the SQL table that supports the structure.

Notice the rows with ParentCategoryID set to NULL are the parent categories.  Each row with a ParentCategoryID is a sub-category and relates to it’s parent category.  The numbers show the level in outline view.

The jQuery Treeview plug-in requires the resulting HTML to be in a particular unordered list (<ul>) element to parse the tree correctly.  Since we don’t know how many levels to expect, we are going to call the MVC user control that renders each node recursively.  This control accepts a single Category item. 

 

We render the name of that category and then check if it has any categories associated with it.  If it does, we recursively render the control again.

In the controller, we are simply passing in the categories where the ParentCategoryID == null.

 

On the host view (Index.aspx), we create the root unordered list with an ID of “treeview” and pass in an array of the Parent categories (categories where the ParentCategoryID == null) from the controller. 

 

Here’s a look at the output WITHOUT the jQuery magic.

Now all we have to do is add the libraries we’re going to use and hook up the Treeview plugin.


Note:  I always use the jQuery script from the Google API’s.  By referencing it from Google’s server, you get to ride along their CDN, compression, etc. capabilities.  As an added bonus, if your user has already visited a site that referenced the same library, the jQuery library would be cached on their browser so they don’t take the hit of having to download it again.  Sweet!

In our jQuery script, we just get the ul’s element ID and call the plugin’s treeview() method with no options. 

 

When we refresh the page, we see jQuery has done its magic and now we have collapsable, rich tree view.

There’s a ton more options in the Treeview plugin that you can check out for yourself, including async loading with ajax, and tons of customizations.  ASP.NET MVC and jQuery – a match made in heaven!

Hope this helps!

I’ve been feverishly adapting and experimenting with the ASP.NET MVC Framework since the very first CTP.  I must admit, it keeps getting better with every release.  This past week was the official BETA release and I’m loving it.

It has been a challenge for developers new to the concepts of MVC to find reference samples to help architect MVC web applications.  For this, I’ve suggested to look towards more mature MVC frameworks such as Ruby on Rails for common patterns in MVC applications.  RoR is not difficult to read and you’ll certainly be able to follow the program logic.  I’ve HIGHLY recommend you take a look at this white paper exploring RESTFul Rails and then see how you can apply it to ASP.NET MVC.

It looks like people are starting to deploy sites based on the ASP.NET MVC Framework already.  I’ve been trying to collect some examples and here’s what I got so far.

If you have a site that you’re developing using the ASP.NET MVC Framework, please let me know about it so I can include it here.