Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's break this down by examining the method signature:</p> <pre><code>MvcHtmlString EditorFor&lt;TModel, TValue&gt;( this HtmlHelper&lt;TModel&gt; html, Expression&lt;Func&lt;TModel, TValue&gt;&gt; expression ) </code></pre> <p>This is using extension method syntax, which means it's adding a method named <code>EditorFor</code> to <code>HtmlHelper</code> such that you can make the call <code>Html.EditorFor</code>. But what we're really interested in is the second parameter, <code>Expression&lt;Func&lt;TModel, TValue&gt;&gt;</code>. That's a pretty complicated parameter, but for now we can ignore the fact that it's an <code>Expression</code>. So simplifying, let's examine:</p> <pre><code>Func&lt;TModel, TValue&gt; expression </code></pre> <p>This means that the argument is <em>any</em> method that has one parameter (of type <code>TModel</code>) and the return type is <code>TValue</code>. You've been using lambdas, which is (essentially) a more concise representation of a method, but it's helpful to just think of it as an ordinary method. So you're lambda is taking a model and returning a model:</p> <pre><code>m =&gt; m </code></pre> <p>That's not as interesting, so let's compare it to a more realistic scenario where you're returning a property off the model:</p> <pre><code>m =&gt; m.MyStringProperty </code></pre> <p>Now let's compare it with an ordinary static method you've declared somewhere:</p> <pre><code>public static class MyStaticClass { public static string Foo(TModel model) { return model.MyStringProperty; } } </code></pre> <p>Although really here it wouldn't be <code>TModel</code> -- it would be whatever you declared your model type via <code>@model</code>. Now, for the sake of discussion, you could have instead used this method in your invocation of <code>EditorFor</code>:</p> <pre><code>Html.EditorFor(MyStaticClass.Foo); </code></pre> <p>So to sum up, lambdas are (for the most part) just a short hand for a regular method. So all you're doing is passing methods around.</p> <p>The last note here is that we are actually using expression trees, which means you aren't actually passing the method, you're passing an object model (an expression tree) that represents the code of the method. This is, essentially, just used to figure out the property name you're using (because usually the lambda would be more like <code>m =&gt; m.MyProperty</code>, not merely <code>m =&gt; m</code>). This is all to avoid magic strings where you refer to the property name by using a string (i.e. "MyProperty").</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload