MVC Tag helpers not working
Asked Answered
P

4

7

I have an issue with tag helpers. All I want to do is create a form that posts data to the controller - basic enough I thought but it doesnt work in my project.

I create a brand new asp.net core web application with default setup and it works there but my project refuses to recognise the tags and act accordingly. Notably the markup doesnt highlight the same or provide info when hovered over so some ref or something fundamental is missing, can anyone advise.

*Default project working with correct highlights enter image description here

*My project - not highlighting the code right or working. Project.json is the exact same so I have no idea whats missing. enter image description here

Pruitt answered 7/6, 2016 at 6:58 Comment(2)
May be it will help. Close the project, -> browse project files-> Delete the .suo file from the explorer -> open and check again -- OR -- try to Unload and Reload project again in solutionChirk
Sadly no. I swear I cannot find a single difference for this issue..Pruitt
P
12

FOUND IT! Microsoft like to change things. So, MVC 6 (asp.net core ...etc) can make use of a _ViewImports.cshtml file in the views folder (feel free to add it yourself if you like me made a project before this existed).

This acts like global import file, and to make the tag helpers available in all your views, you need to add reference here.

Mine now looks like this:

@using Mobile.Models
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

And boom - tags look correct and its work as expected.

Pruitt answered 7/6, 2016 at 12:10 Comment(1)
Nice Find! I think this happened to me because I imported a bunch of views from another project. Non-the-less this did the trick! Thank you!Aeromancy
B
3

You will also face this issue when you create a new Area in the asp.net core MVC project, to resolve it copy ~/Views/_ViewImports.cshtml to ~/AreaName/Views/_ViewImports.cshtml

Bhatt answered 15/3, 2019 at 10:39 Comment(1)
While this works, it can also be solved by simply moving the _ViewImports.cshtml to the root directory of the application (next to Program.cs so to speak).Bise
P
2

Make sure that you MUST specify the assembly name and not the namespace. That was my mistake.

Pily answered 16/4, 2020 at 12:12 Comment(0)
L
0

I'd like to add my solution for this situation. I copied the lines in _ViewImport, then deleted and recreated this file and it worked for me.

Lachance answered 26/3, 2023 at 15:2 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Jennifer

© 2022 - 2024 — McMap. All rights reserved.