tags:

views:

63

answers:

3

What is the difference between mvc HTML.Control and ControlFor (TextBox, checkbox etc)..

+1  A: 

The following article explains the difference in general:

http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx

Simply put, HTML.ControlFor is strongly typed, which allows use of lambda expressions and automatically takes the nameof the property specified as the name/id of the control.

Mahesh Velaga
+1  A: 

The For versions of the HTML helper methods take properties as strongly-typed lambda expressions instead of strings.

For example, the following to statements are equivalent:

<%=Html.TextBox("Description") $>
<%=Html.TextBoxFor(m => m.Description) $>

However, if you rename the Description property, the TextBoxFor call will give a compiler error, whereas the TextBox call will not fail until you visit that page.

SLaks
+1  A: 

One is strongly typed. If you have a view that expects a model of type Customer with a property "CustomerName", you can render the value with either way

<%=Html.Label("CustomerName") %> 

<%=Html.LabelFor(a => a.CustomerName) %> //strongly typed

With the second method (lambda expression), you avoid magic strings. You also have the ability to inspect ModelMetadata to perform additional customizations.

Read about Model metadata here:

http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html

Raj Kaimal