Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tag Helpers not properly rendered in ViewComponent

Tag helpers render properly on ALL my razor pages but not in my ViewComponents

For instance:

<label asp-for="MiddleName" class="control-label"></label>

should render as:

<label class="control-label" for="Contact_MiddleName">Middle Name</label>

but instead simply is included in the page as is.

I have searched the web high and low for hours but can't find any solution. Any pointers on what to try?

like image 533
StillLearnin Avatar asked Jan 20 '18 01:01

StillLearnin


2 Answers

I guess asking the question here gave me an idea.

In my _ViewImports.cshtml file I have

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

which enables tag helpers on all my razor pages. On a whim I added it to the top of my ViewComponent.cshtml and now it works.

I still don't know why I have to add it to each view component file. If someone can give me the answer for that I'll mark your answer as the correct one.

like image 187
StillLearnin Avatar answered Oct 28 '22 15:10

StillLearnin


You have to add in _ViewImports.cshtm tag helper with the name of your project.

@addTagHelper *, ProjectName

like image 38
Baskovli Avatar answered Oct 28 '22 13:10

Baskovli