asp.net-mvc-2

In ASP.NET MVC, how do I display a property name as a label splitting on CamelCase


I know that I have seen this before. I can't remember if it was a C4MVC template demo or an input builder thing! I need to know how I can use the convention of CamelCasing my view model properties and having the "CamelCasedProperty" rendered in the label as "Camel Cased Property". This should be handled by the create new view wizard rather than programatically handling this.


Solution

  • After some poking around I sort of found a lead to what I was interested in doing. Watching this C4MVC video. I am sure that this might already be completed in MVC Contrib as an input builder. However, I am very interested in understanding all the under pinnings so to speak to learn all that I can for my upcoming book "ASP.NET MVC Cookbook" (shameless plug for public review). Here is the solution I came up with that only requires me to rename the Html.LabelFor to Html.LabelFor2 in the generated code form the "create new view":

    I created a method to get the property name from the passed in lambda. I then created another method to parse the property name on the upper case letters contained in the name.

    using System;
    using System.Linq.Expressions;
    using System.Text.RegularExpressions;
    
    namespace FuryPartners.WebClient.Helpers
    {
        public class HelperUtilities
        {
            internal static string PropertyName<T, TResult>(Expression<Func<T, TResult>> expression)
            {
                switch (expression.Body.NodeType)
                {
                    case ExpressionType.MemberAccess:
                        var memberExpression = expression.Body as MemberExpression;
                        return memberExpression.Member.Name;
                    default:
                        return "oops";
                }
            }
    
            internal static string SplitCamelCase(string camelCaseString)
            {
                string output = System.Text.RegularExpressions.Regex.Replace(
                    camelCaseString,
                    "([A-Z])",
                    " $1",
                    RegexOptions.Compiled).Trim();
                return output;
            }
        }
    }
    

    I then extended HtmlHelper to have a LabelFor2 method which builds and passes out the appropriate display format of a property name.

    using System;
    using System.Linq.Expressions;
    using System.Web.Mvc;
    
    namespace FuryPartners.WebClient.Helpers
    {
        public static class LabelHelpers
        {
            public static MvcHtmlString LabelFor2<T, TResult>(this HtmlHelper<T> helper, Expression<Func<T, TResult>> expression)
            {
                string propertyName = HelperUtilities.PropertyName(expression);
                string labelValue = HelperUtilities.SplitCamelCase(propertyName);
    
                string label = String.Format("<label for=\"{0}\">{1}</label>", propertyName, labelValue);
                return MvcHtmlString.Create(label);
            }
        }
    }