开发者

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

开发者 https://www.devze.com 2023-03-20 13:48 出处:网络
I am trying to get below expression value by compiling and invoking but i get some errors and no success till now.

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.Mod开发者_如何学运维el);//problem that is value is null

}


(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.


Use ModelMetadata.FromLambdaExpression Method and then its property Model

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号