D'Arcy from Winnipeg
Solution Architecture, Business & Entrepreneurship, Microsoft, and Adoption

ASP.NET MVC - Interesting HTML.Form Helper Behavior

Thursday, April 24, 2008 9:29 AM

Part of my UG talk the other night involved going over the helper classes that come with the MVC framework. These make it pretty easy to generate HTML that is friendly to your MVC structure without having to hand-bomb the markup yourself. You can read a great article covering them on Rob Conery's blog here.

One of these helpers is Html.Form. This outputs a form tag with the appropriate href for handling any submit action that occurs. In the code below, you can see that I've got three helpers: the form, a textbox, and a submit button. By specifying the controller and action in the form helper, I don't have to specify anything else in the Submit button other than a name...kewl.


Now here's the weird behavior. If you code the above, your page renders like this:


Not really what we were expecting. In fact if you look at the source...


There's no actual closing tag for the Form element. Obviously we need to alter this. You could try putting in the ending form tag...


But the output is identical to the above except you have a closing form tag...the opening tag is still left open.

One of the guys in attendance, Orion (awesome name btw), offered a solution that I *never* would have thought of...the 'Using' statement...


This outputs our screen properly without that 'System.Web.Mvc.SimpleForm' showing up...


And if we look at the source, we see that its actually rendering properly...


So for whatever reason, the using tells it to kick out the end tag as well as close the start tag properly. I'm not sure where else this is applicable with the helpers, but I'm sure we'll see some other situations where this would be applicable.



# re: ASP.NET MVC - Interesting HTML.Form Helper Behavior

>>>You can read a great article covering them on Rob Conery's blog here.<<<

LOL D'Arcy I think your ADD is getting the better of you (I can say that now that I met you :p).

If you read down to the part where I go into how the Form tag works, I talk about using IDisposable (the magic behind the closing tag - Phil's idea) and wrapping the HTML bits in a using statement.

Nice post though :p :):):):) 4/24/2008 1:33 PM | Rob Conery

# re: ASP.NET MVC - Interesting HTML.Form Helper Behavior

LOL...this is what happens when you live for over a week on 4-5 hours of sleep a night while ingesting large amounts of *strange* liquids...


D 4/24/2008 1:41 PM | D'Arcy from Winnipeg

# re: ASP.NET MVC - Interesting HTML.Form Helper Behavior

I wrote about the Disposable pattern years ago: http://haacked.com/archive/2004/05/27/DifficultiesOfLanguageDesign.aspx that it wasn't just intended for disposing of resources. It's actually meant for situations in which you need something to happen at the end of a "block".

The Form helper is unique in that almost all the other helpers render out the full HTML element. But with the form helper, the contents between the <form ...> and </form> is arbitrary.

This usage of Disposable was an idea I had as a way of creating a block.

Problem is, it really sucks for usability. We get all sorts of complaints form the forums. It does not drive people to the "pit of success". We're thinking of changing it. 4/24/2008 4:12 PM | Haacked

Post a comment