I got it working, after several hours of messing with it. The tag helpers weren't working and they were showing up in the HTML source instead of the HTML they were supposed to generate. I found the answer in ChatGPT after pasting all my code in there.
I'll leave this here in case anyone else has the same issue in the future. Here were the steps to fix it:
Yes, you should create a _ViewImports.cshtml file if it doesn't already exist in your project. This file is used to import namespaces and configure tag helpers for Razor views in ASP.NET Core MVC projects.
Here's how you can create a _ViewImports.cshtml file and add the necessary tag helper directive to enable tag helpers:
- Create the File:
- Right-click on the folder where you want to create the
_ViewImports.cshtmlfile (typically in theViewsfolder). - Choose "Add" > "New Item...".
- Select "Razor View Imports" from the list of templates.
- Name the file
_ViewImports.cshtmland click "Add".
- Right-click on the folder where you want to create the
- Add Tag Helper Directive:
- Open the
_ViewImports.cshtmlfile. - Add the following tag helper directive:
- Open the
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
This directive imports all tag helpers from the Microsoft.AspNetCore.Mvc.TagHelpers namespace, allowing you to use them in your Razor views.
- Save the File:
- Save the
_ViewImports.cshtmlfile.
- Save the
Once you've created and saved the _ViewImports.cshtml file with the tag helper directive, it will be applied to all Razor views within the same directory and its subdirectories, allowing you to use tag helpers like asp-for without explicitly importing namespaces or configuring tag helpers in each individual view file.