0

So I have the following helper method that is returning the name of a property given an expression. Its being used in an MVC application to set the ID of a hidden field in a view based on the property it needs to bind to .

public static string GetIDFromPropertyName<T>(Expression<Func<T>> exp)
{
   return (((MemberExpression)(exp.Body)).Member).Name;
}

However what I want is to be able to pass in an expression such as:

GetIDFromPropertyName(() => model.AComplexProperty.AnotherComplexProperty.ASimpleProperty)

And have returned back to me:

AComplexProperty_AnotherComplexProperty_ASimpleProperty

How can I do this?

Update: To be clear, I need to do this on the Server side as opposed to in Razor. The model that is ultimately being passed to my partial view will not contain the property itself, just a string property with the ID to use for a hidden field, so cannot use the built in IdFor extension

Stewart Alan
  • 1,521
  • 5
  • 23
  • 45
  • as explained in [this answer](http://stackoverflow.com/a/16798507/4910910) there is a built in helper for that. – Alexandr Sugak Jun 22 '15 at 14:58
  • To be clearer I need to do this from server side. the model being passed t the view does not contain the property itself, it just contains a string property which it uses to set as a field ID and Name. – Stewart Alan Jun 22 '15 at 15:15
  • could you please extend the question with these details then? Currently it looks like you want to generate input id from nested model expression and IdFor() helper does just that. – Alexandr Sugak Jun 22 '15 at 15:18
  • Have updated question – Stewart Alan Jun 22 '15 at 15:20
  • well it looks strange that you need to do this not in the view (it is view who should know how things are rendered, not model/controller). But if you want to do this you can look into MVC source code for how it generates Ids. Something like this: string id = (new TemplateInfo()).GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(exp)); – Alexandr Sugak Jun 22 '15 at 15:30
  • The view is a div with a bunch of data attributes that get read and turned into mark up by JavaScript to create a complex editing tool. The data is serialized to a hidden field and that is mapped back to the model and it is the ID of that hidden field that needs to map to the model property. The view itself does not get passed the full model, only one of its properties that has the config data it needs, part of which is the string ID to set on the hidden field. – Stewart Alan Jun 22 '15 at 15:33
  • So who generates this config data? Is it generated on the parent view? Why not generate id there using IdFor()? Anyway you can try this to generate Id without html helper instance available: string id = (new TemplateInfo()).GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(exp)); – Alexandr Sugak Jun 22 '15 at 15:45

1 Answers1

0

to get "B_C_D" from this GetIDFromPropertyName(() => a.B.C.D) you can use this code:

public static string GetIDFromPropertyName<T>(Expression<Func<T>> exp)
{
    var members = new List<string>();
    GetIDFromPropertyName(exp.Body, members);
    // members contains {"D", "C", "B", "a"}

    // now simply return the appropiate result, example:
    members.RemoveAt(members.Count -1);
    members.Reverse();
    return members.Aggregate((s1, s2) => s1 + "_" + s2);
}
private static void GetIDFromPropertyName(Expression exp, List<string> members)
{
    var expression = exp as MemberExpression;
    if(expression != null)
    {
        var memberExpression = expression;
        members.Add(memberExpression.Member.Name);
        GetIDFromPropertyName(memberExpression.Expression, members);
    }
}
Alexander Leyva Caro
  • 1,183
  • 1
  • 12
  • 21