views:

57

answers:

2
<% using(Html.BeginForm()) { %> 
... 
<% } %>

OR

<% Html.BeginForm(); %> 
... 
<% Html.EndForm(); %>

What are the advantages/disadvantages of using both ways to use Html.BeginForm?

+4  A: 

I prefer

<% using(Html.BeginForm()) { %> 
... 
<% } %>

Just to make sure I get the warning when it isn't closed. But it isn't the greatest use of the using statement.

You can find more info here. In terms of rendering, there won't be a difference.

Yuriy Faktorovich
I've personally taken to using the second method lately. It is more deliberate and formats better with the surrounding markup. Using an IDisposable to close a tag seems a bit cryptic to me.
Mitch R.
@Mitch: That team also came up with something like Html.Text(color => "red") to render an attribute called color to be red. Not sure what the exact syntax is, or which version it made it into.
Yuriy Faktorovich
+2  A: 

If you are using Spark, don't use either. You can easily add your own extensions, as we've done at Logos.

<logos:using form="Html.BeginForm()">
    Email: <input type="text" name="email" />
</logos:using>

or

<logos:form controller="User" action="Update" routeValues="new { id = 123 }">
    Email: <logos:textbox for="Email" />
</logos:form>
Ryan Riley
There is actually a cleaner way to do this now: bindings! :-) http://sparkviewengine.com/documentation/bindings
Ryan
@Ryan: That is seriously awesome! Thanks for the link!
Ryan Riley