I am creating a html helper method with the following signature:
public static MvcHtmlString MyHelperMethod<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
I know I can get the value of the property being passed as an expression using the following:
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
object modelValue = metadata.Model;
In the same model, I have another property for which I want to retrieve the value in this helper method. How would I retrieve the value of this other property?
You can do that using reflection.
constant “yourProperty” could of course be a parameter of your helper method.
There’s maybe a nicer way, but this one should work.