The first taste of ASP.NET MVC


Genius! Was walking through the tutorials on the new ASP.NET MVC Framework at ScottGu's blog.

Took me about 3 hours to read, walk through the steps, read again, curse, read, walk through it once more again, curse, then lo and behold, what I had finally worked!

While it is not as polished as Ruby on Rails, and with the Subsonic team joining the ASP.NET MVC team, I'm sure a lot of RoR's database migration features will be added and some database-related tasks will be automated so we may have more coffee breaks.

While talking through some of the tutorials, I've encountered some bugs and wanted to share with you some tips that may not have been CLEARLY outlined in the tutorials. Occasionally, while having VS 2008 compile the project after a code change, it would complain in run time about a Html.ActionLink needing an 'new' keyword. Just hit Build > Clean Solution before Start Debugging again.

When you create a Controllers/ProgramsController.cs class, all of the ControllerActions' RenderView text should have relative classes be immediately created in the Programs folder.

In Controllers/ProgramsController.cs

[ControllerAction]
public void Programs()
{
List programs = dc.GetPrograms();
RenderView("Programs", programs);
}

[ControllerAction]
public void List(int TypeId, int? page)
{
List programs = dc.GetProgramsByTypeId(TypeId);
RenderView("ListingByType", programs);
}
...

So you see here, the two RenderView calls: Programs and ListingByType... that means we should immediately manually create a Views/Programs/Programs.cs and Views/Programs/ListingByType.cs classes! (I'm sure that'll be automated once more of the Subsonic stuff has been integrated.)

Next, for every View Content Page you create and there is a database tied in with the application, make sure you add the Generic version of the mapped table or view.

public partial class Detail : ViewPage should be public partial class Detail : ViewPage

When I typed the url, http://localhost:1234/Programs, the page is blank or would complain about not finding an 'Index' controller. What I did to make sure the page is not blank or make it an equivalent of http://localhost:1234/Programs/Programs (listing all programs) is add a ControllerAction of Index() like such:

[ControllerAction]
public void Index()
{
List programs = dc.GetPrograms();
RenderView("Programs", programs);
}
.. a short-cut basically instead of a repetitive URL.

Some learning curve involved with this, but relatively easier than learning the nuances of Windows Presentation Foundation (WPF)! Will post about my experiences with WPF and it isn't pleasant.

Happy em-vee-seeing! (MVCing)