In Asp.Net MVC 1 you might have been creating HTML labels and text boxes by doing something like:
Asp.Net MVC 1 Style:
<div class="searchForm">
<label for="ClientName">Name:</label><%=Html.TextBox("ClientName", Model.ClientName, new { @class = "bigInput" })%>
</div>
Nothing wrong with doing this but in Asp.Net MVC 2 there are some additional helpers: LabelFor and TextBoxFor (there's others but out of scope for this article) which help refactor the above code by using your model properties. Now there seems to be some drawbacks which I’ll explain after.
Asp.Net MVC 2 Style:
<div class="searchForm">
….
<%=Html.LabelFor(model => Model.ClientName) %>
<%=Html.TextBoxFor(model => Model.ClientName, new { @class = "bigInput" })%>
….
</div>
By default using the above code the LabelFor helper will render the label html as “ClientName”, if you want to change the label text (which I imagine you will) you’ll need to use the DisplayName DataAnnotations attribute on your property:
using System.ComponentModel.DataAnnotations;
….
[DisplayName("Client Name")]
public string ClientName { get; set; }
It seems HTML.LabelFor doesn’t take the normal anonymous collection so you can’t easily apply a css class for example, ways around this if you can’t do it in CSS maybe to write a custom override for LabelFor
75f03dd8-8d5d-45f9-b023-899726b71870|0|.0