ASP.Net MVC View Controls
I am spending some time playing and loving the new MVC framework Scott Guthrie, Phil Haack, Hanselman, and crew are working on. Overall I am very excited about this technology and my hat is off to everyone involved.
Most of the examples out there use code in the ASPX view page to build the view, but I am a strongly typed kinda guy and I just can’t live without my user controls for rendering UI. This lead me to find an obtuse little bug in the designer.
I realize this is a trite detail and will likely be fixed in a future release, but I just wanted to drop this out there because it was quite frustrating for me for awhile.
When you create a new ViewPage via the Add > New Item > MVC View Content Page it created the typical ASPX and the code behind *.aspx.cs file. Then when you drop or declare a control on your ASPX page, the control is not declared in the code behind, making it impossible to bind to. We expect the control to be declared in the partial class *.aspx.designer.cs as is typical for a WebForm, but no such file is generated.
That’s ok, you can add your own partial class and Visual Studio sees it as a designer file. Do this before you add controls to your ViewPage and those controls will get auto-declared for you.
In the image below, I have added a partial class for my List page, but not for my Detail page.
The following code is the code I put into the file.
After dropping a literal onto my ViewPage and re-opening my designer class code file, this is what I find.
Smooth as butter.