title: Supporting ViewState in an MVC ViewUserControl
body: <p>I know I know, it's going against the grain. Before everyone wants to lecture about the MVC pattern, this is an extreme measure. I have a ASP.NET control that I must get to work in MVC that requires the ViewState and would appreciate the focus to stay on this. I am aware that I can <a href="http://weblogs.asp.net/rajbk/archive/2010/05/11/running-asp-net-webforms-and-asp-net-mvc-side-by-side.aspx" rel="nofollow">load a classic ASP.NET pages in MVC by a trick of routes</a>, but I need an integral solution that fits within the MVC framework.</p>
  
  <p>There must be a way to extend a ViewUserControl to play along with the ViewState game, or by a filter or attribute, or even mocking a ViewState object by parsing the Request and somehow overriding something early in the pipeline to invoke the LoadViewState. </p>
  
  <p>Alas, I'm afraid I'm not versed enough with classic ASP.NET to know how to do this. The ViewUserControl has the ViewState property derived from the UserControl object along with SaveViewState and LoadViewState methods, so I see some hope in doing this.</p>
  
  <p>Here is what I have so far:</p>
  
  <pre><code>&lt;%@ Control Language="C#" CodeBehind="RadGridExample.ascx.cs" Inherits="MvcApplication5.Views.Shared.RadGridExample" %&gt;
  &lt;form runat="server"&gt;
  
      &lt;telerik:RadScriptManager ID="scriptmanager2" runat="server"&gt;&lt;/telerik:RadScriptManager&gt;
  
      &lt;telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false" AllowMultiRowSelection="true" ShowGroupPanel="true"&gt;
          &lt;MasterTableView TableLayout="Fixed" &gt;
              &lt;Columns&gt;
                  &lt;telerik:GridBoundColumn DataField="Name" HeaderText="Name" DataType="System.String" /&gt;
                  &lt;telerik:GridBoundColumn DataField="Age" HeaderText="Age" DataType="System.String" /&gt;
              &lt;/Columns&gt;
          &lt;/MasterTableView&gt;
          &lt;ClientSettings AllowDragToGroup="true" &gt;
          &lt;/ClientSettings&gt;
      &lt;/telerik:RadGrid&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>The code behind for the partial view:</p>
  
  <pre><code>namespace MvcApplication5.Views.Shared
  {
      public class RadGridExample : ViewUserControl
      {
          protected void Page_Init()
          {
              HomeIndexViewModel viewModel = this.Model as HomeIndexViewModel;
  
              RadGrid grid = this.Controls[0].FindControl("RadGrid1") as RadGrid;
  
              grid.DataSource = viewModel.Animals;
  
              grid.DataBind();
          }
  
          protected override void LoadViewState(object savedState)
          {
              base.LoadViewState(savedState);
          }
  
          protected override object SaveViewState()
          {
              return base.SaveViewState();
          }
      }
  }
  </code></pre>
  
  <p>Funny thing, the SaveViewState gets called, but LoadViewState never does.</p>