Skip to content

ASP.NET Core add Tag Helpers to replace HTML Helpers such as @Html.DisplayFor #3956

Description

@Rick-Anderson

Feature request
In all our on-boarding tutorials (MVC and Razor Pages), new programmers are introduced to both Tag Helpers and HTML Helpers. HTML Helpers are especially complicated. See the Index.cshtml Razor Page

What follows is the complicated explanation of the lambda expression used in the HTML Helper:


Examine the lambda expression used in the following HTML Helper:
@Html.DisplayNameFor(model => model.Movie[0].Title))

The DisplayNameFor HTML Helper inspects the Title property referenced in the lambda expression to determine the display name. The lambda expression is inspected rather than evaluated. That means there is no access violation when model, model.Movie, or model.Movie[0] are null or empty. When the lambda expression is evaluated (for example, with @Html.DisplayFor(modelItem => item.Title)), the model's property values are evaluated.


I was forced to add the lambda explanation because of so many customer questions.

The very next section on the Create Razor Page is all Tag Helpers.

I recommend adding Tag Helpers (TH) to replace all the HTML Helpers that are scaffolded. The scaffolder will need to use the new TH's. This will significantly lower the barrier to entry. New developers can focus on THs and not be forced to learn HTML Helpers simultaneously.

cc @DamianEdwards

The intro tutorials generate a lot of issues because of the complexity of HTML Helpers. For example, Confusing example of DisplayFor

Activity

  1. added
    area-mvc
    on Nov 7, 2018
  2. mkArtakMSFT commented on Nov 7, 2018

    @mkArtakMSFT
    Contributor

    Thanks for contacting us, @Rick-Anderson.
    We have no plans to invest in this area in the near future.

  3. added
    enhancement
    on Nov 7, 2018
  4. vanhoutenbos commented on Nov 5, 2019

    @vanhoutenbos

    Any plans for this (1 year later) I am running into this issue constantly and have not found a proper solution yet.

  5. DamianEdwards commented on Nov 5, 2019

    @DamianEdwards
    Member

    There are still no plans to put this in the product that I know of (@danroth27 for comment) but you could use the TagHelperPack package to get this functionality today.

  6. vanhoutenbos commented on Nov 5, 2019

    @vanhoutenbos

    Thank you for sharing that package, I will look into that!

  7. ghost locked as resolved and limited conversation to collaborators on Dec 5, 2019
  8. Rick-Anderson commented on Feb 19, 2021

    @Rick-Anderson
    ContributorAuthor

    @DamianEdwards @danroth27 HTML helpers are one of the biggest hurdles to onboarding ASP.NET Core UI. The scaffolding engine generates a mix of HTML Helpers and Tag Helpers (TH). Any chance we could eventually get TH replacements so there's only one new thing to learn? V7? V8? Please review my original complaint on complexity of HTML Helpers magic.

    TagHelperPack package doesn't help the tutorials.

    msftbot bot locked as resolved and limited conversation to collaborators on Dec 5, 2019

    @mkArtakMSFT fine to keep closed but can you allow customers to chime in?

  9. mkArtakMSFT commented on Mar 9, 2021

    @mkArtakMSFT
    Contributor

    Reopening this and moving to the Backlog. While we don't plan to work on this at the moment, we will accept community contributions here in case someone is interested to send us a PR.

  10. added this to the Backlog milestone on Mar 9, 2021
  11. Rick-Anderson commented on Mar 9, 2021

    @Rick-Anderson
    ContributorAuthor
  12. DamianEdwards commented on Mar 9, 2021

    @DamianEdwards
    Member

    Please note that my Tag Helpers use reflection to call the appropriate methods on HtmlHelper as the current code in MVC isn't factored such that it can be done through public APIs. If this were done first-class in MVC itself it would hopefully be much more straightforward.

  13. pranavkm commented on Mar 10, 2021

    @pranavkm
    Contributor

    Thanks @DamianEdwards, that's a good starting point to use as a template. In this particular implementation, we would

    • Add GenerateDisplayName to IHtmlGenerator, possibly as a default interface method.
    • Move the body of HtmlHelper.GenerateDisplayName to this interface method.
    • Use this new API commonly from HtmlHelper and the newly minted tag helper.
    • Rinse and repeat for GenerateDisplay and GenerateEditor.
  14. 9 remaining items

  15. removed
    Priority:1
    on Aug 31, 2022
  16. Rick-Anderson commented on Apr 10, 2025

    @Rick-Anderson
    ContributorAuthor

    Closing this as stale. @guardrex will correct me if I'm wrong.

  17. added theissue type on May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-mvcenhancementfeature-mvc-razor-viewsgood first issue

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions