Freshblood
Freshblood

Reputation: 6431

How to get this expression value model => model.Name?

I am trying to get below expression value by compiling and invoking but i get some errors and no success till now.

public static void TextEditorFor<TModel, TProperty>(this System.Web.Mvc.HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression)
{
    var value = expression.Compile().Invoke(html.ViewData.Model);//problem that is value is null

}

Upvotes: 2

Views: 1262

Answers (2)

archil
archil

Reputation: 39501

Use ModelMetadata.FromLambdaExpression Method and then its property Model

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062975

(applies to the original question)

Given the signature, you should just need:

return Convert.ToString(
    expression.Compile().Invoke(modelInstance)
);

You can also do this by inspection of the expression if absolutely needed.

Upvotes: 2

Related Questions