title: Rewriting Html.BeginForm() in MVC 3.0 and keeping unobtrusive javascript
body: <p>This is going to seem like a bit of a silly endeavor, but it's something I want to learn nonetheless. </p>
  
  <p>Right now, in ASP.NET MVC 3.0, you need to use the syntax <code>@using (Html.BeginForm()) {</code> and then later, the <code>}</code> to close a form block to get the fancy new 'unobtrusive javascript', lest you want to write all of it by hand (which is fine).</p>
  
  <p>For some reason (<code>Read: *OCD*</code>) I don't like that. I'd really rather do this.. </p>
  
  <pre><code>@Html.BeginForm()
  &lt;div class="happy-css"&gt;
  &lt;/div&gt;
  @Html.EndForm()
  </code></pre>
  
  <p>Seem stupid yet?  Yeah, well to each their own. I want to understand why it is working how it is and mold it to my liking. So I thought the first place I would start digging is the MVC 3.0 source itself. So I jumped into codeplex to find the <code>BeginForm</code> Extension method. </p>
  
  <p>( <a href="http://aspnet.codeplex.com/SourceControl/changeset/view/63452#288009" rel="noreferrer">http://aspnet.codeplex.com/SourceControl/changeset/view/63452#288009</a> )</p>
  
  <p>So now I am a little confused as to how to begin achieving my goal. Reading through the code, I discovered that they all go down to a root method (not surprising, as most extension methods seem to be hierarchical methods all reaching down into a single one to avoid redundancy).</p>
  
  <pre><code>private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary&lt;string, object&gt; htmlAttributes) {
              TagBuilder tagBuilder = new TagBuilder("form");
              tagBuilder.MergeAttributes(htmlAttributes);
              // action is implicitly generated, so htmlAttributes take precedence.
              tagBuilder.MergeAttribute("action", formAction);
              // method is an explicit parameter, so it takes precedence over the htmlAttributes.
              tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
  
              HttpResponseBase httpResponse = htmlHelper.ViewContext.HttpContext.Response;
              httpResponse.Write(tagBuilder.ToString(TagRenderMode.StartTag));
              return new MvcForm(htmlHelper.ViewContext.HttpContext.Response);
          }
  </code></pre>
  
  <p>What I am not seeing here is how this method relates to the unobtrusive javascript. If I simply type out ..</p>
  
  <p><code>&lt;form action="/Controller/Action" method="post"&gt;</code></p>
  
  <p>and then put in my validation like so... </p>
  
  <p><code>@Html.ValidationSummary(false)</code></p>
  
  <p>I do not get the unobtrusive javascript. But if I use </p>
  
  <p><code>@using (Html.BeginForm()) {</code> then I do. I've even examined the generated markup and I really can't find the difference. </p>
  
  <p>Now then it gets strange. If I just type in ... </p>
  
  <p><code>@Html.BeginForm()</code> and then put all of my form code, the form <strong>works</strong> and I get the unobtrusive javascript, but I have to manually type in <code>&lt;/form&gt;</code> at the end. <code>@Html.EndForm()</code> doesn't work. But ontop of that, I now get the text <code>System.Web.Mvc.Html.MvcForm</code> written to the output stream right beneath the <code>&lt;form action="/Controller/Action" method="post"&gt;</code> html.</p>
  
  <p>Can someone enlighten and/or help me?</p>