简体   繁体   中英

How to get part of model expression points to in strongly typed HtmlHelper extension

ok say i have the following model:

public class bar{
    public string bar {get; set; }
}

public class foo{
    public bar mybar{get; set;}
    public string anotherproperty{get; set;}
}

And in the UI i want to do this:

@Html.MyWhackyHelperFor(x=>x.bar)

which uses :

public static MvcHtmlString MyWhackyHelperFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression){
//how can i get a the actual bar object here?
}

how do i get at the actual part of the model being refered to?

您需要将表达式编译为方法,然后调用该方法:

TValue val = expression.Compile()(htmlhelper.ViewData.Model);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM