title: Creating reusable HTML view components using Razor in ASP.NET MVC
body: <p>I have a Razor helper function that creates a re-usable HTML panel that saves me writing the same HTML over and over again.</p>
  
  <pre><code>@helper DefaultPanel(string panelTitle) {
      &lt;div class="panel"&gt;
          &lt;div class="panel-logo"&gt;&lt;img src="/logo.png"&gt;&lt;/div&gt;
              &lt;div class=panel-inner"&gt;
                  &lt;p class="panel-title"&gt;@panelTitle&lt;/p&gt;
                  &lt;div class="panel-content"&gt;
                      /* Can I pass content to be rendered in here here? */
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>I'm wondering, is it possible to re-use this helper to fill <code>.panel-content</code> with more HTML to allow further flexibility and code reuse - <strong>similar</strong> to something like below:</p>
  
  <pre><code>@LayoutHelpers.DefaultPanel("Welcome back") {
      &lt;div class="panel-content-inner"&gt;
          &lt;p&gt;Welcome back, please select from the following options&lt;/p&gt;
          &lt;a href="#"&gt;Profile&lt;/a&gt;
          &lt;a href="#"&gt;My Defails&lt;/a&gt;
      &lt;/div&gt;
  }
  </code></pre>
  
  <p>Whilst using .NET MVC I've noticed the <code>Html.BeginForm()</code> does a similar thing when wrapping the code within the <code>@using</code> statement within the <code>Html.BeginForm</code> declaration, like so:</p>
  
  <pre><code>@using (Html.BeginForm("Index", "Login", FormMethod.Post))
  {
      &lt;div&gt;This content gets rendered within the &lt;form&gt;&lt;/form&gt; markup.&lt;/div&gt;
  }
  </code></pre>
  
  <p>But can this done using <code>@helper</code> methods? If not, is it possible to create a <code>HtmlHelper</code> extension to do a similar thing the way the <code>Html.BeginForm()</code> method does?</p>
  
  <p>You can do a very similar thing using the <code>@section</code> syntax as seen <a href="http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx" rel="noreferrer">here</a></p>
  
  <p><img src="https://i.stack.imgur.com/eJAtp.png" alt="enter image description here"></p>
  
  <p>This seems like something that would be really useful to be able to do, and odd that there's no easy way to do it on a component level.</p>