ASP.NET MVC Framework (Part 4): Handling Form Edit and Post Scenarios
page 8 of 15
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 57194/ 93

ASP.NET MVC HTML Helpers

One approach we could use to generate our dropdowns would be to manually create our own <% %> for-loop containing if/else statements within the HTML.  This would give us total control over the HTML - but would make the HTML messy.

A much cleaner approach that you can alternatively use is to take advantage of the "Html" helper property on the ViewPage base class.  This is a convenient object that exposes a set of HTML Helper UI methods that automate HTML UI generation.  For example, earlier in this post we used the Html.ActionLink helper method to generate <a href=""> elements:

Figure 21

The HtmlHelper object (as well as the AjaxHelper object - which we'll talk about in a later tutorial) have been specifically designed to be easily extended using "Extension Methods" - which is a new language feature of VB and C# in the VS 2008 release.  What this means is that anyone can create their own custom helper methods for these objects and share them for you to use. 

We'll have dozens of built-in HTML and AJAX helper methods in future previews of the ASP.NET MVC Framework.  In the first preview release only the "ActionLink" method is built-into System.Web.Extensions (the assembly where the core ASP.NET MVC framework is currently implemented).  We do, though, also have a separate "MVCToolkit" download that you can add to your project to obtain dozens more helper methods that you can use with the first preview release. 

To install the MVCToolkit HTML Helper methods, simply add the MVCToolkit.dll assembly to your project references:

Figure 22

Re-build your project.  And then the next time you type <%= Html. %> you'll see many, many more additional UI helpers that you can use:

Figure 23

To build our HTML <select> dropdowns we could use the Html.Select() method.  Each method comes with overloaded method versions - all with full intellisense inside our views:

Figure 24

We could update our "New" view to use the Html.Select options to display dropdownlists that use the CategoryID/SupplierID properties as the values and CategoryName/SupplierName as the display text using the code below:

Figure 25

This will generate the appropriate <select> HTML markup for us at runtime:

Figure 26

And give end-users an easier way to pick the Product Category and Supplier on our /Products/New screen:

Figure 27

Note: because we are still posting a CategoryID and SupplierID value to the server, we do not need to update our ProductsController Create Action at all to support this new drop-downlist UI - it will just work.


View Entire Article

User Comments

Title: Stuck / adding Creat & New to my Controller.cs   
Name: Casey
Date: 2009-07-29 4:08:45 PM
Comment:
Hi,
I'm a newbie as if you don't hear that all day long.

I have setup a project that is to allow me to post information to a SQL database, and view the updated information. I can view just fine, however I'm stuck on where in my code to add the Create, and New Methods. If you would like to look at my code I can post it or email it.

Thank you,

Casey,
Title: text search for an table in asp.net mvcframework   
Name: senthil
Date: 2009-02-07 8:42:22 AM
Comment:
i want to make an text search on a column in a table.
for exampe i type a name in the text box and click a button then it should search the name in the column .

hope u help me

thanks in advance






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 7:41:09 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search