I am building a helper method that takes a model expression and does something with its value, as in:
Html.EditorFor(m => m.Name)
public static MvcHtmlString MyMethod<TModel, TProperty>(this HtmlHelper<TModel> html,
Expression<Func<TModel, TProperty>> expression)
TModel dataModel = html.ViewData.Model;
MemberExpression mexp = expression.Body as MemberExpression;
PropertyInfo pinfo = mexp.Member as PropertyInfo;
string currentValue = dataModel != null ? pinfo.GetValue(dataModel) as string : null; //exception on this line
[do something with the value...]
Object does not match target type.
model.SomeChild.Name is not a property of your model class. There is no such thing as a nested property.
Name is a property get operation on the object returned by the
SomeChild get operation. It's a bit like
In your code you would need to recursively unpack the
MemberExpressions and build up a string from them.