Stop using DisplayFor in Razor
- Dominant language
- C#
- Stars
- 818
- Forks
- 260
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 10
Description
I am trying to reconcile the [output of the generator](https://docs.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/page#the-create-delete-details-and-edit-pages-1) with the [samples recommended by Razor](https://docs.microsoft.com/en-us/aspnet/core/razor-pages/#the--directive-3) and I cannot. The former uses just `@`, the latter uses `@Html.DisplayFor` which is [not a Razor thing](https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/views/creating-custom-html-helpers-cs#understanding-html-helpers).
Also, the notation `@Html.DisplayFor(modelItem => item.Property)` is abusive; `DisplayFor` was intended to be used in form view to display the properties of the model, not in list view to display whatever there is to display. That means you need to call `DisplayFor` for the *item model*, not for the *list model*, which probably means you need to delegate the row display to a separate template in order to be clean, preferably using a tag helper for the purpose. This is what I think, I have not tested it yet though.
The calls to `@Html.DisplayFor` should be removed from the Razor templates. Instead, a way to customise the rendering in the model that does not require these calls should be provided.
Contributor guide
Assessment
This issue has not been assessed yet.