dotnet / dotnet/Scaffolding

Stop using DisplayFor in Razor

Open
#1,806 3 comments 0 reactions 0 assignees View on GitHub
area-scaffolding customer-reported type-enhancement
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.