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:

MyViewUserControl.ascx

<ul>

<% foreach(Sponsor s in (Sponsor[])ViewData["Sponsors"]) { %>      

<li><%= s.Name %></li>   

<% } %>

</ul> 

And then you would call it from the ViewPage:

MyViewPage.aspx

<% this.RenderUserControl("~/views/shared/MyViewUserControl.ascx") %>

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:

<mvc:Call Controller="Sponsors" Action="Show" />

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! 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Brian Sullivan

From one geek to another...