ASP.NET MVC Beta Released
page 9 of 14
by Scott Guthrie
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 58949/ 96

HTML Helper Cleanup

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.

View Entire Article

User Comments

Title: how about validation   
Name: geocine
Date: 2010-12-15 7:42:28 PM
Values are not mapped but validations are still executed.
Title: sikat ang pinoy   
Name: renanorola
Date: 2010-02-05 8:35:24 PM
Creating silverlight on mvc are now easy. Thanks for the tutorials I like how you have presented the information in full detail. Keep up the great work
Title: ASP.NET MVC   
Name: Ranganathan
Date: 2009-02-09 1:54:22 AM
Today's beta release is a step closer to the final ASP.NET MVC 1.0 product. While not 100% feature complete, we think the major subsystems are all getting really close to being done, and that the quality level is now pretty good.

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024  |  Page Processed at 2024-04-23 1:52:39 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search