19 January 2010 - 11:23 PM / by Dominic Pettifer. 2 Comments for Strongly Typed <label> elements in ASP.NET MVC 2.
Cool C# Snippets - Generate strongly typed <label> elements in a way that is (IMHO) better than the default method built into ASP.NET MVC 2.
In ASP.NET MVC version 2.0 they introduced support for strongly typed <input /> controls. Whereas before you'd have to type something along the lines of:
<%= Html.TextBox("EmailAddress", Model.EmailAddress) %>The problem being that you could mistype "EmailAddress" and the error wouldn't get picked up until runtime, or you might rename the property "EmailAddress" on your ViewModel, and refactoring tools built into Visual Studio wouldn't pick up the "EmailAddress" string used in the TextBox() method. Now in ASP.NET MVC 2 there is support for strongly typed HTML Helper methods, where any errors can be picked up at compile time:
<%= Html.TextBoxFor(m => m.EmailAddress) %>
Read more at Scott Guthrie's blog (http://weblogs.asp.net/scottgu/archive/2010/01/10/asp-net-mvc-2-strongly-typed-html-helpers.aspx).
In MVC 2 they also added support for strongly typed <label> elements as well (sort of):
<%= Html.LabelFor(m => m.EmailAddress) %>
...which outputs the following...
<label for="EmailAddress">EmailAddress</label>
The benefits of this method being that it will reliably generate the 'for' attribute that points to the <input /> element's ID its paired with, eg.
<label for=”EmailAddress”>EmailAddress</label> <input type=”text” id=”EmailAddress” name=”EmailAddress” />
However, Html.LabelFor has no overload that allows you to specify the actual <label> element text, it defaults to the property name (EmailAddress) which looks ugly. You can set the label text via DataAnnotations on the ViewModel class itself eg.
public class AccountView
{
//-- snip (other properties) --//
[Required]
[DisplayName(“Your e-mail address:”)]
public string EmailAddress { get; set; }
}...and you'll get:
<label for="EmailAddress">Your e-mail address:</label>
The problem is some people, like me, would consider the <label> text to be a View/Presentation concern specified in the .aspx views, and that it shouldn’t go inside your Model classes. You could just type <label for="EmailAddress"> directly, but then you lose the benefit of strong typing the Input's ID. So I wrote an Html Extension method that generates the control Ids based on the ViewModel Property:
using System;
using System.Linq.Expressions;
using System.Web.Mvc;
namespace MvcLibrary.Extensions
{
public static class HtmlExtensions
{
public static MvcHtmlString FieldIdFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string inputFieldId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName);
return MvcHtmlString.Create(inputFieldId);
}
}
}Which you can use in your Views like this:
<label for="<%= Html.FieldIdFor(m => m.EmailAddress) %>">Your e-mail address:</label> <%= Html.TextBoxFor(m => m.EmailAddress) %>
I suppose one could just write an overload for the LabelFor() method that takes a string parameter for the label text. However, just generating the input ID has other benefits. For one you’re more in control of the markup if you want to add attributes (class, id etc.) to the <label> element. Secondly the Html.FieldIdFor() method can be used elsewhere you need the dynamically generated control ID, such as in calling JavaScript functions that make the ID of input controls.
<script> $.CoolJQueryFunction('<%= Html.FieldIdFor(m => m.EmailAddress) %>'); </script>
Thank you.
I'm using your helper when I want to show validation message inside label:
<label for="<%= Html.FieldIdFor(x => x.Article.Title) %>">Title: <%= Html.ValidationMessageFor(x => x.Article.Title, "*") %></label>
Posted on 10 July 2010 - 2:53 AM / by Denis
[url=http://www.nikeam.com]Air shoes[/url]
[url=http://www.nikeam.com]Nike Air shoes[/url]
<a href="http://www.nikeam.com">Air shoes</a>
<a href="http://www.nikeam.com">Nike Air shoes</a>
Posted on 27 May 2011 - 8:40 AM / by Air shoes
Thanks for very useful information for my college research.
Posted on 25 January 2012 - 5:02 PM / by essay writing company
<p>In autumn, quite a lot of people would like to travel, then to own a <b><a href="http://www.louisvuittonhandbags-mall.com/" title="Louis Vuitton Handbags">Louis Vuitton Handbags</a></b> would be a necessary. Here we on sale some new versions which are lager enough for you to put the clothes and other things. We are a great<b> <a href="http://www.louisvuittonhandbags-mall.com/" title="Louis Vuitton Outlet">Louis Vuitton Outlet</a></b> that have been on sale online quite many years and earn high reputation. Do you also want to own the <b><a href="http://www.louisvuittonhandbags-mall.com/" title="louis vuittin purses">louis vuittin Outlet store</a></b>, then shop here. </p>
Posted on 27 September 2011 - 4:57 AM / by Louis Vuitton Outlet store
http://www.jordannikeheels2011.com
http://www.nikefreeshop2012.com
http://www.nikeheelsjordan2011.com
http://www.nikeheelweb2011.com
http://www.nikeheeltw.com
http://www.nikefreeshoes2012.com
http://www.uggsales2011.com
http://www.venteugg2011.com
http://www.monclersoutlet2011.com
http://www.monclerveste2011.com
Posted on 5 December 2011 - 7:17 AM / by nike high heels
Search engine optimization (SEO Company USA) can be defined as an activity that undertakes web promotion of websites/portals and web pages. The quality SEO services ensure ranking of a website in popular search engines such as Yahoo, Google, Bing and MSN.
Search engine optimization (SEO Company USA) can be defined as an activity that undertakes web promotion of websites/portals and web pages. The quality SEO services ensure ranking of a website in popular search engines such as Yahoo, Google, Bing and MSN.
[url=http://www.seosoftwareservices.com]SEO India[/url]
Posted on 3 January 2012 - 12:46 PM / by WEB Development India
of the [url=http://www.abercrombie-nederland.nl/]abercrombie brussel[/url] [url=http://www.abercrombie-nederland.nl/]Abercrombie Nederland[/url] light, but also disturb my life, endless, endless [url=http://www.abercrombie-nederland.nl/]abercrombie parijs[/url] gratitude kindness.
good hot cup of ... [url=http://www.abercrombie-nederland.nl/]Abercrombie and Fitch[/url]
Posted on 28 November 2011 - 5:10 AM / by Abercrombie Nederland
Thanks for sharing these useful information! Hope that you will continue doing nice article like this.
Posted on 30 December 2011 - 5:56 AM / by t shirt printing essex
I enjoyed every little bit part of it and I will be waiting for the new updates.Thanks for sharing....
Posted on 4 January 2012 - 8:40 AM / by love sms
such a humorous But <b><a href="http://www.11jordan.net/">air jordan 2011</a></b> how to deal with some real headaches in <b><a href="http://www.11jordan.net/air-jordan-retro-8-C39.html">jordan 8</a></b> <b><a href="http://www.11jordan.net/air-jordan-retro-7-C38.html">retro Jordan 7</a></b> children <b><a href="http://www.11jordan.net/air-jordan-retro-7-C38.html">cheap Jordan 7</a></b> <b><a href="http://www.11jordan.net/">jordan release dates 2011</a></b> of parents scolding, perhaps... <b><a href="http://www.11jordan.net/air-jordan-retro-7-C38.html">buy Jordan 7</a></b>
Posted on 29 January 2012 - 6:50 AM / by canada goose jackets
For once I'm going to disagree with you.
IMO, your View Model (not Domain Model) are a direct View/Presentation concern. (Actually I see it as a new generation of "code behind")
What you do in your ViewModel are what you want expressed in your view, while this View Model get mapped (with an OOM or manually) to a Domain Model where there should be no presentation logic at all.
Just my 20 cents, but typing <label for="<%= Html.FieldIdFor(m => m.EmailAddress) %>">Your e-mail address:</label> seems like an aweful lot of work if you have 1000 labels all over your application.
1 View = 1 View Model = Any Number of/part of Domain Models. No?
Posted on 14 May 2010 - 6:09 AM / by Krokonoster
Newest styles of <a href="http://www.christianlouboutinreplicacl.com/"><strong>christian louboutin high heels</strong></a> in hot sale now, <a href="http://www.christianlouboutinreplicacl.com/"><strong>Christian Louboutin Knockoffs</strong></a> shoes sale now, buy <a href="http://www.christianlouboutinreplicacl.com/"><strong>christian louboutin replica</strong></a> in our online uk store .your shoes sales prices will save.
Posted on 22 September 2011 - 2:16 AM / by christian louboutin replica
Silverlight Usage Statistics from Google Analytics. (from the blog Add Silverlight Tracking to your Google Analytics )
And YouTube still auto-fucking-plays videos!! This is TWO-THOUSAND-AND-FUCKING-TWELVE FFS!!!
about 20 hours ago from webOn a side-note, YouTube's commenting system is god-awful atrocious dreadful horrible horrible horrible!! Constant meaningless error messages
about 20 hours ago from webJavaScript is slow mmmkay http://t.co/NbB4eQjw - Actually, no, it's not http://t.co/kpGEIoPO #nodejs
about 20 hours ago from webTFS: It's super expensive, so it must be brilliant, right? Like Sharepoint #tekpubtfstitlesuggestion
5:22 PM February 3rd from web