I've followed the instructions of Mac's answer here and I've built my own custom extension:
public static class HtmlHelperExtensions
{
public static string HtmlIdNameFor(
this HtmlHelper htmlHelper,
System.Linq.Expressions.Expression> expression)
{
return (GetHtmlIdNameFor(expression));
}
private static string GetHtmlIdNameFor(Expression> expression)
{
if (expression.Body.NodeType == ExpressionType.Call)
{
var methodCallExpression = (MethodCallExpression)expression.Body;
string name = GetHtmlIdNameFor(methodCallExpression);
return name.Substring(expression.Parameters[0].Name.Length + 1).Replace('.', '_');
}
return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1).Replace('.', '_');
}
private static string GetHtmlIdNameFor(MethodCallExpression expression)
{
var methodCallExpression = expression.Object as MethodCallExpression;
if (methodCallExpression != null)
{
return GetHtmlIdNameFor(methodCallExpression);
}
return expression.Object.ToString();
}
}
I've imported my application's namespace
and finally I can use my code like this:
m.Customer.Name)%>