Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在强类型HtmlHelper扩展中获取部分模型表达式点_C#_Asp.net Mvc 3_Html Helper - Fatal编程技术网

C# 如何在强类型HtmlHelper扩展中获取部分模型表达式点

C# 如何在强类型HtmlHelper扩展中获取部分模型表达式点,c#,asp.net-mvc-3,html-helper,C#,Asp.net Mvc 3,Html Helper,好的,假设我有以下型号: public class bar{ public string bar {get; set; } } public class foo{ public bar mybar{get; set;} public string anotherproperty{get; set;} } 在UI中,我要执行以下操作: @Html.MyWhackyHelperFor(x=>x.bar) 它使用: public static MvcHtmlStri

好的,假设我有以下型号:

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

public class foo{
    public bar mybar{get; set;}
    public string anotherproperty{get; set;}
}
在UI中,我要执行以下操作:

@Html.MyWhackyHelperFor(x=>x.bar)
它使用:

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?
}
public static MvcHtmlString MyWhackyHelperFor(此HtmlHelper HtmlHelper,表达式){
//我怎样才能得到一个实际的酒吧对象在这里?
}

如何获取所引用模型的实际部分?

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

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

好极了。谢谢-was TValue val=expression.Compile()(htmlhelp.ViewData.Model);确切地说,ffr:)