Click here to Skip to main content
15,891,253 members
Articles / Web Development / HTML

CheckBoxList(For) - A missing MVC extension (no longer supported)

Rate me:
Please Sign up or sign in to vote.
4.82/5 (44 votes)
4 Feb 2014CPOL6 min read 771K   15K   128  
Extends MVC HtmlHelper class so you can create POSTable checkbox list.
using System;
using System.Linq.Expressions;
using System.Web.Mvc;

namespace MvcCheckBoxList.Library {
  internal static class ToProperty_Helper {
    /// <summary>
    /// Convert lambda expression to property name
    /// </summary>
    /// <typeparam name="TModel">Current ViewModel</typeparam>
    /// <typeparam name="TItem">ViewModel Item</typeparam>
    /// <param name="propertyExpression">Lambda expression of property value</param>
    /// <returns>Property value string</returns>
    internal static string toProperty<TModel, TItem>
      (this Expression<Func<TModel, TItem>> propertyExpression) {
      // v.1.4
      return ExpressionHelper.GetExpressionText(propertyExpression);

      // v.1.3c
      //var lambda = propertyExpression as LambdaExpression;
      //var expression = lambda.Body.ToString();
      //return expression.Substring(expression.IndexOf('.') + 1);

      // v.1.2
      //// return property name only
      //var lambda = propertyExpression as LambdaExpression;
      //MemberExpression memberExpression;
      //if (lambda.Body is UnaryExpression) {
      //  var unaryExpression = lambda.Body as UnaryExpression;
      //  memberExpression = unaryExpression.Operand as MemberExpression;
      //}
      //else
      //  memberExpression = lambda.Body as MemberExpression;
      //var propertyInfo = memberExpression.Member as PropertyInfo;
      //return propertyInfo.Name;
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
Coding is awesome!

Comments and Discussions