title: Sharing data between HtmlHelpers
body: <p>I'm looking for a way to share data between HtmlHelpers. The goal is to set configuration once instead of passing a parameter with each call. In the example below, I'm setting the form type to determine the grid configuration:</p>
  
  <pre class="lang-cs prettyprint-override"><code>@{ Html.SetFormType(HtmlHelpers.FormType.Boxed); }
  @Html.FormItemFor(m =&gt; m.LoginName)
  @Html.FormItemFor(m =&gt; m.Password)
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>public static void SetFormType(this HtmlHelper helper, FormType formType)
  {
      helper.ViewBag.FormType = formType;
      helper.ViewContext.ViewBag.FormType = formType;
  }
  </code></pre>
  
  <p>Setting <code>ViewBag</code> does not persist through subsequent <code>@Html</code> calls. Setting <code>ViewContext.ViewBag</code> persists, but is it safe to do so?</p>
  
  <p>The existing method  <code>EnableUnobtrusiveJavaScript</code> is set in an instance of the <code>ScopeCache</code> class, which in turn is added to <code>HttpContext.Items</code>. Perhaps that's also a candidate to store my data?</p>