title: Razor Func<object, object> mixed with MvcHtmlString
body: <p>I'm no guru in Razor syntax, but trying to build a generic library using a fluent-style à là Telerik's GUI components.</p>
  
  <p>I have the following pieces (approximately):</p>
  
  <pre><code>public static MyBox Box(this HtmlHelper helper)
  {
      return new MyBox(helper.ViewContext);
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// http://geekswithblogs.net/shaunxu/archive/2010/04/10/lt-gt-htmlencode-ihtmlstring-and-mvchtmlstring.aspx
  /// &lt;/summary&gt;
  public class MyBox : IHtmlString
  {
      private readonly ViewContext _viewContext;
      private string _content;
      private string _title;
  
      public MyBox(ViewContext viewViewContext)
      {
          _viewContext = viewViewContext;
      }
  
      /// &lt;summary&gt;
      /// See: http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx
      /// &lt;/summary&gt;
      /// &lt;param name="value"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public MyBox Content(Func&lt;object, object&gt; value)
      {
          _content = value.DynamicInvoke(_viewContext).ToString();
          return this;
      }
  
      /// &lt;summary&gt;
      /// See: http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx
      /// &lt;/summary&gt;
      /// &lt;param name="values"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public MyBox Content(params Func&lt;object, object&gt;[] values)
      {
          foreach (var value in values)
          {
              _content += value.DynamicInvoke(_viewContext).ToString();
          }
          return this;
      }
  
      public MyBox Content(MvcHtmlString content)
      {
          _content = content.ToString();
          return this;
      }
  
      public MyBox Title(string title)
      {
          _title = title;
          return this;
      }
  
      public string ToHtmlString()
      {
          using (var stringWriter = new StringWriter())
          {
              WriteHtml((TextWriter)stringWriter);
              return stringWriter.ToString();
          }
      }
  
      public void Render()
      {
          using (var writer = new HtmlTextWriter(_viewContext.Writer))
              WriteHtml(writer);
      }
  
      protected virtual void WriteHtml(TextWriter writer)
      {
          writer.WriteLine("&lt;!-- START BOX --&gt;");
          writer.WriteLine("&lt;h1&gt;" + _title + "&lt;/h1&gt;));
          writer.WriteLine(_content);
          writer.WriteLine("&lt;!-- END BOX --&gt;");
      }
  }
  </code></pre>
  
  <p>I also have a set of Html extension methods that return MvcHtmlString's. One example is (simplified):</p>
  
  <pre><code>public static class GuiElementExtensions
      {
          private const string FieldContainerHeadingTemplate = @"
  &lt;tr&gt;&lt;th style=""text-align:left"" colspan=""2""&gt;{0}&lt;/th&gt;&lt;/tr&gt;
  ";
  
      public static MvcHtmlString GuiFieldContainerHeading(this HtmlHelper helper, string text)
          {
              return new MvcHtmlString(String.Format(FieldContainerHeadingTemplate, text));
          }
  }
  </code></pre>
  
  <p>Then, in my .cshtml file, I do the following:</p>
  
  <pre><code>  @using (Html.BeginForm())
         {
             @(Html.Gui()
                        .Box()
                        .Title("Hello World!")
                        .Content(
                                  @&lt;h1&gt;Hello World! This is the cool Gui.Box&lt;/h1&gt; 
                         )
          )
   }
  </code></pre>
  
  <p>Which calls <code>public MyBox Content(Func&lt;object, object&gt; value)</code>, and works.
  Similarly, when I try the following:</p>
  
  <pre><code>  @using (Html.BeginForm())
         {
             @(Html.Gui()
                        .Box()
                        .Title("Hello World!")
                        .Content(
                                  Html.GuiFieldContainerHeading("SubHeading 1")
                         )
          )
   }
  </code></pre>
  
  <p>It happily calls <code>public MyBox Content(MvcHtmlString content)</code> and works as expected.</p>
  
  <p>But, however, when I try to do the following, I can't wrap my head around how the Razor compiler engine works. How do I get it to return the sum of</p>
  
  <ul>
  <li><code>@&lt;h1&gt;Hello World! This is Gui().Box()&lt;/h1&gt;</code>  (which is a Func)</li>
  <li><code>Html.GuiFieldContainerHeading("SubHeading 1")</code>  (which is an MvcHtmlString)</li>
  </ul>
  
  <p>as either one object (be it a Func, MvcHtmlString, whatever, or a list of objects? I would like to write generic Razor syntax inside the parameter list to the Content function in my MyBox class, like this:</p>
  
  <pre><code>  @using (Html.BeginForm())
         {
             @(Html.Gui()
                        .Box()
                        .Title("Hello World!")
                        .Content(
                                  @&lt;h1&gt;Hello World! This is Gui().Box()&lt;/h1&gt; 
                                  Html.GuiFieldContainerHeading("SubHeading 1")
                  Html.TextBoxFor(model =&gt; model.Name)
                  @&lt;h2&gt;Hello there!&lt;/h2&gt;
                         )
          )
   }
  </code></pre>
  
  <p>Am I on the right track whatsoever, or is there a much simpler way of doing what I want? I want to gather all the "common gui elements" in our system in a common DLL, so not every developer in my organization needs to reinvent the wheel on each project.</p>
  
  <p>Any help appreciated.</p>
  
  <hr>
  
  <p>OK, next problem:</p>
  
  <p>I have generalized the Box into a Container, and created two subclasses, Box and HighlightArea. However, using the following code, the Razor compiler kicks me with the message</p>
  
  <blockquote>
    <p>Inline markup blocks (@&lt;p&gt;Content&lt;/p&gt;) cannot be nested. 
    Only one level of inline markup is allowed.</p>
  </blockquote>
  
  <p>The code not working, is:</p>
  
  <pre><code> @(Html.Gui()
                .Box()
                .Title("BoxTitle")
                .Content(@&lt;text&gt;
                          &lt;h1&gt;Hello World! This is the box content&lt;/h1&gt; 
                          @Html.GuiFieldContainerHeading("This is the heading")
                          @(Html.Gui().HighlightArea()
                                .Content(
                                  @Html.ValidationSummary()
                                  @&lt;h1&gt;Jalla&lt;/h1&gt;
                                )
                          )
                         &lt;/text&gt;
                 )
  </code></pre>
  
  <p>Do we have a workaround for this? Or is my approach not feasible?</p>