Posts
202
Comments
1112
Trackbacks
51
MVC ComponentController vs. ViewUserControl

UPDATE: The component controller was removed from the MVC framework before the RTM release. For an updated version of this post, click here.

At some point when creating a web app, you're going to want some reusable UI components.  This might be because you want the same visual UI snippet repeated more than once on a single page or it might be because you want to use the same component on multiple pages.  In a traditional ASP.NET web app, typically you would use a User Control for this type of thing.  In MVC you still have the option of using a User Control but you also have the option of rendering your UI snippet with a ComponentController.  So the question is, when do you use which?  There are pros and cons to each.

The most common scenario for a user control is that you pass in the Model from the containing view page.  For example, consider a AddressEditor user control that you use multiple times on a page to edit Contacts - once for Home address and once for Work address.  It might look like this:

<%=Html.RenderUserControl("~/Views/Home/AddressEditor.ascx", new AddressViewData(ViewData.Model.Contact.HomeAddress))%>

This is fine for those simple cases but what happens when you want your user control to have its own self contained logic?  For example, what if you're buildng a portal or a portal-like website where you want to have several self-contained "widgets" in your page.  You don't want the parent controller having to keep track of the models for 10 different widgets and then have to pass them in to each one.  A good answer to this is to use a ComponentController rather than a View user control.  A ComponentController can render its own views and the containing page can just look like this:

   1:  <div id="firstWidget" class="widget">
   2:      <%=Html.RenderComponent<MvcWidget.Controllers.WidgetCompController>(c => c.Widget1()) %>
   3:  </div>

In this case, the controller class basically looks like most any other controller class with the exception that it inherits from ComponentController:

   1:  public class WidgetCompController : ComponentController
   2:  {
   3:      public void Widget1()
   4:      {
   5:          IWidgetManager widgetManager = new Widget1Manager();
   6:          List<Foo> list = widgetManager.GetFooData();
   7:          RenderView("Widget1", list);
   8:      }
   9:  }

However, there still are some uses for view user controls here.  Let's say you want to render your widgets via an AJAX call.  One feature of the "normal" MVC controllers is that the views rendered can be either aspx or ascx.  Rendering an ascx user control from an MVC Controller would be a good choice when you want to implement the AJAX HTML Message Design Pattern where you're returning only an HTML snippet from the server rather than an entire page.  In this case, we can render our user controls with an AJAX jQuery call.  So if we had a couple of HTML divs (called "secondWidget" and "thirdWidget") and a WidgetController class with actions methods Widget2() and Widget3(), we could simply implement this simple jQuery function:

   1:  <script type="text/javascript">
   2:      $(function() {
   3:          $('#secondWidget').load('/Widget/Widget2');
   4:          $('#thirdWidget').load('/Widget/Widget3');
   5:      });
   6:  </script>

These are some rough ideas to get you started.  The complete code sample can be downloaded here.

posted on Sunday, July 6, 2008 9:29 PM Print
Comments
Gravatar
# re: MVC ComponentController vs. ViewUserControl
sheraz
5/18/2009 7:31 AM
Please can you give some more detail how to implement this??


"Rendering an ascx user control from an MVC Controller would be a good choice when you want to implement the AJAX HTML Message Design Pattern where you're returning only an HTML snippet from the server rather than an entire page. "

thanks in advance
Gravatar
# re: MVC ComponentController vs. ViewUserControl
Steve
5/18/2009 8:41 AM
@sheraz - The complete code sample from the blog post above can be downloaded from this link: https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=michelotti&ReleaseId=1259 and it includes an example of the AJAX HTML Message design pattern. However, this was based on an older version of MVC so you'll have to take that into account because the RenderComponent() method doesn't exist anymore in the RTM of the MVC framework. you'll have to look into using the RenderPartial() method instead but same principles apply.

Post Comment

Title *
Name *
Email
Comment *  
 

View Steve Michelotti's profile on LinkedIn

profile for Steve Michelotti at Stack Overflow, Q&A for professional and enthusiast programmers




Google My Blog

Tag Cloud