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