Click here to Skip to main content
13,598,498 members
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 20 Jan 2012
Licenced CPOL

Convert View Model property name to string => made easy! (MVC3)

, 3 Apr 2012
Rate this:
Please Sign up or sign in to vote.
Tutorial on how to create simple extension that converts view model propety name to string


Have you ever needed to get a simple property name from MVC View Model?

"Why would I need that?" - you may ask. The answer is pretty simple - if you have jQuery/JavaScript code that works with form elements created from view model, you can have those element names strongly typed in your JavaScript. Also it might be useful in some content output scenarios as well. 

What we need 

Let's say we have this class:

public class SomeClass {public class SomeClass {
  public string SomeProperty { get; set; }
And we have a view model that implements the class above:
public class SomeViewModel{
  public SomeClass SomeClass { get; set; }

Then on our MVC View, which inherits from our view model 'SomeViewModel', you have a text box on your form: 

@Html.TextBoxFor(model => model.SomeClass.SomeProperty)  

Which creates html:

<input id="SomeClass_SomeProperty" name="SomeClass.SomeProperty" type="text"> 

And, for example, you want to add a JQuery UI Datepicker to that text box:


But if we rename 'SomeProperty' property or 'SomeClass' class, we'll have to manually rename references to it from jQuery. Which might be unpleasant with a large amount of jQuery code. 

So, we need to have this 'Someclass_SomeProperty' created in a strongly typed manner. How do we do that? 

Create HtmlHelper Extension 

We will create a simple extension of the MVC HtmlHelper class. So, first let's create an extension class anywhere in the project:

using System;
using System.Linq.Expressions;
using System.Web.Mvc;
public static class MvcExtensions {
  public static string GetPropertyName<TModel>
   (this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, object>> propertyNameExpr) {
    return ExpressionHelper.GetExpressionText(propertyNameExpr).Replace('.', '_');
Then on the view, when we want to add a jQuery UI datepicker to textbox, we do this:
$('#@Html.GetPropertyName(model => model.SampleClass.SampleProperty)').datepicker();  

And the output is:


Which is just what we wanted!

Additionally, if we just need to get the name of the property, but not the class, add this extension to class MvcExtensions

public static string GetPropertyNameShort<TModel>(this HtmlHelper<TModel> htmlHelper,
  Expression<Func<TModel, object>> propertyNameExpr) {
  var expression = ExpressionHelper.GetExpressionText(propertyNameExpr);
  return expression.Substring(expression.LastIndexOf('.') + 1);

So now, the same code on the view will return this:


I found this extension to be very useful in large complex applications, especially after you use ReSharper to rename classes/properties. 


A little amendment - this code from extensions above:

var expression = ExpressionHelper.GetExpressionText(propertyNameExpr); 

Will work fine, but will fail when either class or property is null. If you need it to work on null properties/values, change line above like this: 

var lambda = propertyNameExpr as LambdaExpression;
var expression = lambda.Body.ToString().Replace(")", "");
return expression.Substring(expression.IndexOf('.') + 1); 

And that's all folks, thank you for reading! 


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


About the Author

Web Developer
United States United States
Coding is awesome!

You may also be interested in...


Comments and Discussions

SuggestionIndexer Values Pin
Paul Pemberton4-Feb-14 7:25
memberPaul Pemberton4-Feb-14 7:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180621.3 | Last Updated 3 Apr 2012
Article Copyright 2012 by Mikhail-T
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid