Strongly Typed Helpers in ASP.NET MVC 2

Posted by: Malcolm Sheridan , on 6/16/2010, in Category ASP.NET MVC
Views: 38717
Abstract: The following article demonstrates how to use the new strongly typed helpers in ASP.NET MVC 2.
ASP.NET MVC 2 has made some big leaps and bounds since it burst onto the scene recently. A nice addition to the framework is strongly typed helpers. These provide two keys features:
- Allow for intellisense support in the view
- Allow you to use lambda expressions
Being able to use a lambda expression against the model being passed into the view is incredibly compelling. Out of the box there are a number of strongly typed helpers such as:


These helper methods allow you to pass in a lambda expression, so if you have a property called GivenName, you could do this:
<%:Html.TextBoxFor(model =""> model.GivenName) %>
In ASP.NET MVC 1, you had to do this:
<%= Html.TextBox("GivenName")%>
By passing in a string representation, you're opening yourself up to a potential problem. By using a string, you can misspell the property. You will still be able to compile your code, but you'll get a runtime error whilst debugging. By using strongly typed helpers, the problem vanishes.
To see this in action, I'm going to create a small ASP.NET MVC 2 website. If you haven't got Microsoft Visual Studio 2010, you can download the Express edition here. I've created a Person class to hold some data. This way you'll see the strongly typed helpers in action when you render an Edit view. Here's the Person class:
public class Person
    public string Name { get; set; }
    public int Age { get; set; }
    public DateTime DOB { get; set; }
    public bool Married { get; set; }
Next I've created an action in my controller that creates a new Person object and passes that to the view. To see the new strongly typed helpers, you need to render an Edit view. Here's the action code:
public ActionResult Index()
      var person = new Person
             Name = "Malcolm",
             Age = 30,
             DOB = new DateTime(2000, 1, 17),
             Married = true
       return View(person);
Public Function Index() As ActionResult
      Dim person = New Person With {.Name = "Malcolm", .Age = 30, .DOB = New DateTime(2000, 1, 17), .Married = True}
         Return View(person)
End Function
The next step is to create the View. You'll need to create an Edit View, and have it pass in the Person object. This way your view will be strongly typed and you can use lambda expressions in your view:
And here's the code for your view:
<% using(Html.BeginForm()){%>
    <div class="editor-label">
      <%:Html.LabelFor(model =""> model.Name) %>
    <div class="editor-field">
      <%:Html.TextBoxFor(model =""> model.Name) %>
      <%= Html.TextBox("Name")%>
      <%:Html.ValidationMessageFor(model =""> model.Name) %>
    <div class="editor-label">
      <%:Html.LabelFor(model =""> model.Age) %>
    <div class="editor-field">
      <%:Html.TextBoxFor(model =""> model.Age) %>
      <%:Html.ValidationMessageFor(model =""> model.Age) %>
    <div class="editor-label">
      <%:Html.LabelFor(model =""> model.DOB) %>
    <div class="editor-field">
      <%:Html.TextBoxFor(model =""> model.DOB, String.Format("{0:g}", Model.DOB)) %>
      <%:Html.ValidationMessageFor(model =""> model.DOB) %>
    <div class="editor-label">
      <%:Html.LabelFor(model =""> model.Married) %>
    <div class="editor-field">
      <%:Html.CheckBoxFor(model =""> model.Married) %>
      <%:Html.ValidationMessageFor(model =""> model.Married) %>
      <input type="submit" value="Save" />
You'll notice in the code above the use of the strongly typed helpers. All of them use lambda expressions, so there are no more syntax errors now. Just having the ability to use a lambda expression this way is incredibly useful. The Married property is a boolean type, and that was rendered as a check box, meaning the property type has been correctly matched to its HTML element:
<%:Html.CheckBoxFor(model =""> model.Married) %>
This is one new feature of ASP.NET MVC 2 that I really like. I'll be addressing new features in the framework in upcoming posts.
The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Malcolm Sheridan is a Microsoft awarded MVP in ASP.NET, a Telerik Insider and a regular presenter at conferences and user groups throughout Australia and New Zealand. Being an ASP.NET guy, his focus is on web technologies and has been for the past 10 years. He loves working with ASP.NET MVC these days and also loves getting his hands dirty with jQuery and JavaScript. He also writes technical articles on ASP.NET for SitePoint and other various websites. Follow him on twitter @malcolmsheridan

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!