How to Change ASP.NET MVC ValidationMessageFor from <span> to <label>

Wednesday, 28 July 2010 11:49 by SyntaxC4

When it comes to providing a more usable interface for your user, I find that labels are the key. Being able to style a label as an error message and position it where you need it for your user is great, the added functionality being, if the user clicks on the label it lands their cursor nicely in the corresponding field [provided you have filled in the ‘for’ attribute].

When it comes to ASP.NET MVC, Microsoft made the choice to go with a <span> tag instead of the <label>.  I’m not sure why they made this decision, and please feel free to comment below if I'm breaking an html rule here, but a label for error validation just seems to be a no brainer to me.

If you feel the same here is a snippet of Code I created to be change a ValidationMessageFor Extension Method into a Method that provides the label error messaging.

     using System;
     using System.Linq.Expressions;
     using System.Text.RegularExpressions;
     using System.Web.Mvc;
     using System.Web.Mvc.Html;
 
     public static class ValidationExtensions
     {
         public static MvcHtmlString ValidationMessageLabelFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, string errorClass = "error")
         {
             string elementName = ExpressionHelper.GetExpressionText(expression);
             MvcHtmlString normal = html.ValidationMessageFor(expression);
             if (normal != null)
             {
                 string newValidator = Regex.Replace(normal.ToHtmlString(), @"<span([^>]*)>([^<]*)</span>", string.Format("<label for=\\"{0}\\" $1>$2</label>", elementName), RegexOptions.IgnoreCase);
 
                 if (!string.IsNullOrWhiteSpace(errorClass))
                     newValidator = newValidator.Replace("field-validation-error", errorClass);
 
 
                 return MvcHtmlString.Create(newValidator);
             }
             return null;
         }
     }
 

I know that Regular Expression can cause some performance issues, and I don’t call myself a RegEx Master, so if you find a more efficient way to do this please Contact Me, and I will be sure to post the code change.

Remember: “friends don’t let friends __doPostBack();”

Happy Coding!

Joining a Great Team at BITtelligent

Tuesday, 20 July 2010 16:25 by SyntaxC4

I am pleased to announce that I have decided to join the Cambridge, Ontario based Software Development company BITtelligent Software and Media, Inc. BITtelligent Software is owned by Mark Dykun [@markdykun], a member of both Canada’s Technology Triangle .NET Users Group and Guelph Coffee and Code.

Providing Desktop and Web Development using Microsoft Technologies, SalesLogix Customization, Mobile Development on Blackberry and Windows Mobile, and other Emerging Technologies

 

BITtelligent can develop a wide range of Software projects, including Windows Desktop Applications, Web Applications (leveraging ASP.NET MVC2), Rich Internet Applications (leveraging Silverlight 4), Mobile Software Development for Blackberry or Windows Mobile (and soon Windows Phone), Sage SalesLogix Customizations, and other emerging technologies.

I will be playing my strengths in my role handing Web Development, using both ASP.NET MVC, and Silverlight. If you’re looking for a custom Web Portal with SalesLogix integration, or need a Silverlight based Rich Internet Application, be sure to Contact BITtelligent Software.

Having Cassini Crash while using ASP.NET MVC

Tuesday, 15 June 2010 07:00 by SyntaxC4

This will be a short entry, but I think that it might save some people some time when building out ASP.NET MVC websites.

If you find that Cassini is crashing when you are testing out your ASP.NET MVC Application here is one more thing to check. Look at your master page file, it is generally a good idea not to touch any of the code that gets generated by the Visual Studio Designer, but from time to time you may find yourself playing around with the code in order to minimize the amount of content is being pushed out to the client. 

The Masterpage file still requires a <head> with the runat="server" attribute. This is required to render the content in the Placeholders within it. With that said, if you are to remove the runat="server" you will experience an error, in my case, the cassini web server would crash when ever I tried requesting a page with the invalid masterpage.

With any luck, this will help you out!

Happy Coding!

Tags:   ,
Categories:   Microsoft
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | RSS

About SyntaxC4:

  • Cory Fowler
  • Guelph, Ontario
  • English
  • SyntaxC4

SyntaxC4 Tweets:

Posts by Date:

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

Archive:

Advertisements:

Tag Cloud:

Favourite Publishers:

Apress Daily Deal
Apress Daily Deal

Blog Roll: