title: ASP.NET MVC : Good Replacement for User Control?
body: <p>I found user controls to be incredibly useful when working with ASP.NET webforms. By encapsulating the code required for displaying a control with the markup, creation of reusable components was very straightforward and very, very useful.</p>
  
  <p>While MVC provides convenient separation of concerns, this seems to break encapsulation (ie, you can add a control without adding or using its supporting code, leading to runtime errors). Having to modify a controller every time I add a control to a view seems to me to integrate concerns, not separate them. I'd rather break the purist MVC ideology than give up the benefits of reusable, packaged controls.</p>
  
  <p>I need to be able to include components similar to webforms user controls throughout a site, but not for the entire site, and not at a level that belongs in a master page. These components should have their own code not just markup (to interact with the business layer), and it would be great if the page controller didn't need to know about the control. Since MVC user controls don't have codebehind, I can't see a good way to do this.</p>
  
  <p><strong>Update</strong>
  FINALLY, a good (and, in retrospect, obvious) way to accomplish this.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  
  namespace K.ObjectModel.Controls
  {
      public class TestControl : ViewUserControl
      {
          protected override void Render(System.Web.UI.HtmlTextWriter writer)
          {
              writer.Write("Hello World");
              base.Render(writer);
          }
      }
  }
  </code></pre>
  
  <p>Create a new class which inherits <code>ViewUserControl</code></p>
  
  <p>Override the <code>.Render()</code> method as shown above.</p>
  
  <p>Register the control via its associated ASCX as you would in a webForm:</p>
  
  <p><code>&lt;%@ Register TagName="tn" TagPrefix="k" Src="~/Views/Navigation/LeftBar.ascx"%&gt;</code></p>
  
  <p>Use the corresponding tag in whatever view or master page that you need:</p>
  
  <p><code>&lt;k:tn runat="server"/&gt;</code></p>
  
  <p>Make sure your .ascx inherits your new control:</p>
  
  <p><code>&lt;%@ Control Language="C#" Inherits="K.ObjectModel.Controls.TestControl" %&gt;</code></p>
  
  <p>Voila, you're up and running. This is tested with ASP.NET MVC 2, VS 2010 and .NET 4.0. </p>
  
  <p>Your custom tag references the ascx partial view, which inherits from the TestControl class. The control then overrides the <code>Render()</code> method, which is called to render the view, giving you complete control over the process from tag to output.</p>
  
  <p>The difference between using this approach and calling <code>Html.RenderPartial()</code> or `Html.RenderAction()' is adding the control to a view is done with a webforms-like tag, which is not only more comfortable for designers, but keeps them from having to be aware of controller names and methods. The name of the control class is isolated to the ASCX, also making it easier to drop these in an assembly and reuse them across separate projects.</p>
  
  <p>Some may say that this violates SoC, but I believe that this approach is functionally equivalent to tying a partial view and a controller together while maintaining clean markup. It should be clear, however, that it is still up to the developer to keep only presentation-related logic in the control Business and data access logic still belong in their respective layers.</p>