Asp.net mvc 3 Html.DisplayFor(或其他HTMLHelper)
我有一个很基本的问题,但我真的不明白。它来了 如果我有一个视图模板文件(.cshtml),并且有这样一个代码行:Asp.net mvc 3 Html.DisplayFor(或其他HTMLHelper),asp.net-mvc-3,razor,Asp.net Mvc 3,Razor,我有一个很基本的问题,但我真的不明白。它来了 如果我有一个视图模板文件(.cshtml),并且有这样一个代码行: @Html.DisplayFor(m => m.CurrentPage.MainBody) public static MvcHtmlString DisplayFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>&
@Html.DisplayFor(m => m.CurrentPage.MainBody)
public static MvcHtmlString DisplayFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
public static MvcHtmlString DisplayFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression
)
{
...
}
如果我查看DisplayFor的声明,它如下所示:
@Html.DisplayFor(m => m.CurrentPage.MainBody)
public static MvcHtmlString DisplayFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
public static MvcHtmlString DisplayFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression
)
{
...
}
然后(n=>n%2==1)的上下文是逻辑,lamdba表达式用于以数字形式计算每个元素
但是在上面使用@Html.DisplayFor(m=>m.CurrentPage.MainBody)的情况下,这里的上下文是什么?我指的是什么?在这个特定的视图中,它是否以某种方式“神奇地”连接到@model?(在本例中为@model PageViewModel
)
总之,m在表达式(m=>m.CurrentPage.MainBody)中指的是什么?是否假定它通过@model引用视图中提供的模型
这里的lambda表达式(m=>m.CurrentPage.MainBody)如何知道m是什么
Html.DisplayFor帮助程序的定义如下:
@Html.DisplayFor(m => m.CurrentPage.MainBody)
public static MvcHtmlString DisplayFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
public static MvcHtmlString DisplayFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression
)
{
...
}
Html属性的类型为HtmlHelper
,因此DisplayFor助手了解您的模型
基本上,当您有一个强类型视图时,Html.DisplayFor(m=>m.CurrentPage.MainBody)
是Html.DisplayFor(m=>m.CurrentPage.MainBody)
的快捷方式,编译器可以从上下文推断通用参数,您不需要显式编写它们
这里的lambda表达式(m=>m.CurrentPage.MainBody)如何知道m是什么
Html.DisplayFor帮助程序的定义如下:
@Html.DisplayFor(m => m.CurrentPage.MainBody)
public static MvcHtmlString DisplayFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
int oddNumbers = numbers.Count(n => n % 2 == 1);
public static MvcHtmlString DisplayFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression
)
{
...
}
Html属性的类型为HtmlHelper
,因此DisplayFor助手了解您的模型
基本上,当您有一个强类型视图时,
Html.DisplayFor(m=>m.CurrentPage.MainBody)
是Html.DisplayFor(m=>m.CurrentPage.MainBody)的快捷方式
编译器可以从上下文推断泛型参数,您不需要显式编写它们。非常感谢。现在我明白了!非常感谢。现在我明白了!