Question

In MVC if you want to create an editor for a property or a display for you property you do something like that:

@Html.EditorFor(m=> m.MyModelsProperty);
@Html.DisplayFor(m=> m.MyModlesProperty);

Why do we have to pass a delegate why can't we just pass the model's property directlly? e.g.:

@html.EditorFor(Model.MyModlesProperty);
Was it helpful?

Solution

The reason for this is because of Metadata. You know, all the attributes you could put on your model, like [Required], [DisplayName], [DisplayFormat], ... All those attributes are extracted from the lambda expression. If you just passed a value then the helper wouldn't have been able to extract any metadata from it. It's just a dummy value.

The lambda expression allows to analyze the property on your model and read the metadata from it. Then the helper gets intelligent and based on the properties you have specified will act differently.

So by using a lambda expression the helper is able to do a lot more things than just displaying some value. It is able to format this value, it is able to validate this value, ...

OTHER TIPS

I'd like to add, that besides the Metadata and making the Html helper strongly typed to the Model type, there's another reason:

Expressions allow you to know the name of the property without you hard coding strings into your project. If you check the HTML that's produced by MVC, you'll see that your input fields are named "ModelType_PropertyName", which then allows the Model Binder to create complex types that are passed to your Controller Actions like such:

public ActionResult Foo(MyModel model) { ... }

Another reason would be Linq to SQL. Expression Trees are the magic necessary to convert your Lambdas to SQL queries. So if you were to do something like:

Html.DisplayFor(p => p.Addresses.Where(j => j.Country == "USA"))

and your DbContext is still open, it would execute the query.

UPDATE
Stroked out a mistake. You learn something new every day.

The first example provides a strongly-typed parameter. It forces you to choose a property from the model. Where the second is more loosely-typed, you could put anything in it, even something that isn't valid property of the model.

Edit: Surprisingly, I couldn't find a good example/definition of strong vs loose typing, so I'll just give a short example regarding this.

If the signature was @html.EditorFor(string propertyName); then I could make a typo when typing in the name and it would not be caught until run-time. Even worse, if the properties on the model changed, it would NOT throw a compiler error and would again not be detected until run-time. Which may waste a lot of time debugging the issue.

On the other hand with a lambada, if the model's properties changed you would get a compiler error and you would have to fix it if you wanted to compile your program. Compile-time checking is always preferred over run-time checking. This removes the chance of human error or oversight.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top