asp.net mvc 3 - Html.DisplayFor ( or other htmlhelpers ) -
i have question quite basic don't it. here comes.
if have view template file ( .cshtml ) , have codeline this:
@html.displayfor(m => m.currentpage.mainbody)
if on declaration displayfor looks this:
public static mvchtmlstring displayfor<tmodel, tvalue>(this htmlhelper<tmodel> html, expression<func<tmodel, tvalue>> expression);
so extension method takes look parameter tmodel , tvalue seems generic ( , apparently can send in lambda look expression).
how can lambda look here ( m => m.currentpage.mainbody ) know m is?
if have lamdba look this:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; int oddnumbers = numbers.count(n => n % 2 == 1);
then context (n => n % 2 == 1) logic, lamdba look used evaluate each element in numbers.
but in case above @html.displayfor(m => m.currentpage.mainbody) context here? m referring to? somehow "magically" connected @model in particular view? ( in case is@model pageviewmodel<articlepage>
").
so sum up, m referring in look ( m => m.currentpage.mainbody ) ? assumed somehow refering model provided in view via @model?
how can lambda look here ( m => m.currentpage.mainbody ) know m is?
the html.displayfor helper defined this:
public static mvchtmlstring displayfor<tmodel, tvalue>( htmlhelper<tmodel> html, expression<func<tmodel, tvalue>> look ) { ... }
notice how helper can only invoked on typed htmlhelper<tmodel>
. if not have typed view model cannot utilize html.displayfor
helper because html
on invoking extension method htmlhelper
, not htmlhelper<tmodel>
.
so within view when have model:
@model myviewmodel
the html property of type htmlhelper<myviewmodel>
displayfor helper knows model.
basically when have typed view, html.displayfor(m => m.currentpage.mainbody)
shortcut html.displayfor<myviewmodel, thetypeofyourmainbodyproperty>(m => m.currentpage.mainbody)
compiler infer generic arguments context , don't need write them explicitly.
asp.net-mvc-3 razor
No comments:
Post a Comment