Rewriting Html.BeginForm () in MVC 3.0 and saving unobtrusive javascript

It will seem a little stupid thing, but that is what I want to know nonetheless.

Right now, in ASP.NET MVC 3.0, you need to use the @using (Html.BeginForm()) { and then } syntax to close the form block to get a fantastic new “unobtrusive javascript” so you don't write all this manually (this is good).

For some reason ( Read: *OCD* ) I don't like this. I would rather do it.

 @Html.BeginForm() <div class="happy-css"> </div> @Html.EndForm() 

Seems still stupid? Yes, it’s good for everyone. I want to understand why it works the way it is, and shape it to your liking. So I thought that the first place I will start digging is the source of MVC 3.0. So I went into codeplex to find the BeginForm Extension method.

( http://aspnet.codeplex.com/SourceControl/changeset/view/63452#288009 )

So, now I'm a little confused about how to start achieving my goal. After reading the code, I found that they all go to the root method (not surprising, since most extension methods seem to be hierarchical methods, all of which boil down to one, to avoid redundancy).

 private static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> 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); } 

I do not see here how this method relates to unobtrusive javascript. If I just type ..

<form action="/Controller/Action" method="post">

and then enter my check this way ...

@Html.ValidationSummary(false)

I do not get unobtrusive javascript. But if I use

@using (Html.BeginForm()) { , then I do it. I even studied the generated markup, and I really can't find the difference.

Now it is getting weird. If I just type ...

@Html.BeginForm() and then put all my form code, the form works , and I get unobtrusive javascript, but I need to manually enter </form> at the end. @Html.EndForm() does not work. But now, I now get the text System.Web.Mvc.Html.MvcForm written to the output stream directly under the <form action="/Controller/Action" method="post"> html.

Can someone enlighten and / or help me?

+6
unobtrusive-javascript asp.net-mvc-3 html-helper html.beginform
source share
2 answers

The answer to your main question (for example, how to use BeginForm / EndForm syntax) is as follows:

 @{ Html.BeginForm(...); } <div> content</div> @{ Html.EndForm(); } 

Unfortunately, the Razor syntax is now a bit more verbose when calling helpers that write on the output (unlike most helpers who just return an html fragment). You could probably make it simpler by writing your own extension methods as follows:

 public static IHtmlString FormBegin(this HtmlHelper helper, ...) { helper.BeginForm(...); return new HtmlString(""); } public static IHtmlString FormEnd(this HtmlHelper helper) { helper.EndForm(); return new HtmlString(""); } 
+8
source share

The reason I work, I believe that the BeginForm method returns an MvcForm object, not an html, it writes html directly to the page. When the used block ends, it writes a closing end tag. As a result, you will see the text System.Web.Mvc.Html.MvcForm . You must put the private tag there manually because the MvcForm object is not located.

The usage syntax is like this:

 @{ MvcForm mf = Html.BeginForm(); /* writes directly to html stream */ } <div class="happy-css"> </div> @{ mf.Dispose(); /* like calling mf.EndForm() */ } 
0
source share

All Articles