A lot is being made of Microsoft’s Model View Controller (or MVC) framework at the moment.

I have only spent a couple of days working with it so far but it does look promising. If you are a TDD convert or just interested in solid OOP principles such as separation of concerns it may well be right up your street.

As with all change, the emergence of this new approach to asp.net programming has caused its fair share of controversy and debate but that’s for another day. Lets just say there are those who are yet to be convinced of it’s value and those who are already sold on it.

For today though I want to focus on the View part of the equation.

Interestingly I think Scott Guthrie may have had a bit of a revelation one day when he was posting about MS AJAX on his blog. Whether MVC was already in development or not, this post does seem to lean neatly into what we’re now all talking about.

Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel Scenarios 

I have recently adapted the technique mentioned in that article for an AJAX web site. Lets look at some code (heavily based on Scott’s original post).

  1. public static string RenderView(string path, object[] data)
  2.   {
  3.       HtmlForm tempForm = new HtmlForm();
  4.  
  5.       Page pageHolder = new Page();
  6.       pageHolder.EnableEventValidation = false;
  7.       pageHolder.EnableViewState = false;
  8.  
  9.       UserControl viewControl = (UserControl) pageHolder.LoadControl(path);
  10.  
  11.       if (data != null)
  12.       {
  13.           Type viewControlType = viewControl.GetType();
  14.           PropertyInfo propertyInfo = viewControlType.GetProperty("Data");
  15.  
  16.           if (propertyInfo != null)
  17.           {
  18.               propertyInfo.SetValue(viewControl, data, null);
  19.           }
  20.           else
  21.           {
  22.               throw new Exception("View file: " + path + " does not have a public Data property");
  23.           }
  24.       }
  25.  
  26.       tempForm.Controls.Add(viewControl);
  27.       pageHolder.Controls.Add(tempForm);
  28.  
  29.       StringWriter output = new StringWriter();
  30.  
  31.       try
  32.       {
  33.           HttpContext.Current.Server.Execute(pageHolder, output, false);
  34.       }
  35.       catch (Exception ex)
  36.       {
  37.           Debug.WriteLine(ex.Message);
  38.           throw;
  39.       }
  40.  
  41.       string outputToReturn = output.ToString();
  42.  
  43.       outputToReturn = outputToReturn.Substring(outputToReturn.IndexOf("
    "));
  44.       outputToReturn = outputToReturn.Substring(0, outputToReturn.IndexOf(""));
  45.  
  46.       int StartPoint = outputToReturn.IndexOf(");
  47.       if (StartPoint >= 0)
  48.       {
  49.           int endPoint = outputToReturn.IndexOf("
", StartPoint) + 6;
  •           outputToReturn = outputToReturn.Substring(endPoint);
  •       }
  •  
  •       return outputToReturn;
  •   }