Just a quick note here. I was playing around with ASP.NET MVC today, and I got to thinking, how does one encapsulate both behavior and markup (a la UserControls in WebForms) when using MVC? Well, there’s a thing called a ViewUserControl that you can use in your ViewPages, but all that really does is encapsulate the markup and rendering. You’d use them like this:
And then you would call it from the ViewPage:
If you use a typed ViewUserControl, you can feed the object to the RenderUserControl method as an additional parameter rather than going to the ViewData collection, but that’s not what gets to me.
The controller that renders MyViewPage.aspx is still responsible for retrieving all the data that MyViewUserControl.ascx uses. So, everywhere that I want to display that list of sponsors, I have to remember to write the code to go get it. That’s not really reuse, is it?
There was a comment on Rob Conery’s blog back in January that I ran across that suggested something like this:
That way, there can be one controller that’s responsible for pulling that Sponsor data from the data access layer. I’m not sure if this is possible; I’m not familiar enough with the way MVC works to know if a second (or third, or nth) controller can be created and called once the main controller has started rendering MyViewPage. From an end-user perspective, something like that would be great, since it would let us truly encapsulate every aspect of what that ViewUserControl is about. Here’s hoping!