Today's beta has some miscellaneous cleanup improvements to
the HTML helpers (in general this is a tricky area - since there are so many
overload combinations to get right).
Html.Form -> Html.BeginForm
One of the usability changes made with today's beta was to
rename Html.Form() to Html.BeginForm() and to support two modes of using it -
one leveraging a using statement, and the other leveraging an explicit
Html.EndForm() helper method. The reason we've moved to support both of these approaches is that we've seen a lot of questions/confusion in the forums
around how the using statement works for this scenario (the pattern is
unfamiliar to a lot of developers).
Below are two examples that demonstrate how we can implement
the above create screen scenario (complete with validation error message UI)
using the two different form approaches:
Approach 1: Using Statement with Html.BeginForm():
The below approach uses the IDisposable pattern with the
using keyword in VB and C# to auto-terminate the </form>:
Figure 21
Approach 2: Explicit Html.BeginForm() and
Html.EndForm():
The below approach uses an explicit EndForm() call to close
the </form>:
Figure 22
Developers can use whichever they feel most comfortable with
- both approaches logically do the exact same thing.
Many HTML Helper Methods Moved to be Extension Methods
One change we made with today's beta was to move many of the
Html helper methods to be extension methods that live under the
System.Web.Mvc.Html namespace (previously they were just instance methods on
the HtmlHelper class). We did a similar thing with the AJAX helper
methods in "Preview 5" (they now live in the System.Web.Mvc.Ajax
namespace).
These changes don't impact intellisense in the view markup
(we by default automatically reference the namespace in the web.config file so
it works just like before - although if you are migrating an app from preview 5
you'll need to add the namespace yourself to web.config, read the release notes
for steps on how to-do this). If you have standalone classes/tests that
use the helper methods make sure to add the appropriate "using"
statement to import them.
The reason we moved the helper methods to be extension
methods instead of instance methods was to provide developers with more
flexibility to add/remove/replace our built-in implementations (as well as to
give ourselves more flexibility in the future). If you want to override the
HTML rendering of a method you can now easily do so - and still keep the same
method code/signature in your markup.