TagHelper for ASPNET CORE ViewComponent not working
Asked Answered
J

1

11

I'm in the process of getting to grips with ASPNET CORE [version 1.1, using VS2017 Community, version 15.1 (26403.3)]I've created a working ViewComponent called UserDetails (abbreviated below):

namespace AdminConsole.ViewComponents
{
[ViewComponent(Name = "UserDetails")]
public class UserDetailsViewComponent : ViewComponent
{ ...does stuff... }

and I can invoke it successfully in a view by using

@await Component.InvokeAsync("UserDetails")

I'd rather invoke it by using a TagHelper, but it just ain't happening. I've trawled through SO and other helpful pages, and whilst others seem to get it to work, I can't.

I've added the line @addTagHelper "*, AdminConsole" in _ViewImports.cshtml and <vc:user-details></vc:user-details> in the view I want to render the VC in, and it does not render; I don't get an error, it just doesn't render.

If I change the TagHelper declaration to @addTagHelper *, AdminConsole (without the speech marks) it also does not render or error.

If I try both combinations as above and try <vc:UserDetails></vc:UserDetails> i.e. without the kebab-case, it does not render or error.

My _ViewImports.cshtml is as follows

@using AdminConsole
@using AdminConsole.Models
@using AdminConsole.Models.AccountViewModels
@using AdminConsole.Models.ManageViewModels
@using Microsoft.AspNetCore.Identity
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@inject Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration TelemetryConfiguration
@addTagHelper "*, AdminConsole"

Using the full namespace for the VC in the @addTagHelper declaration (AdminConsole.ViewComponents) produces a cannot resolve TagHelper error.

I think I've tried all permutations, and documentation from official and community sources only suggest what I've tried (and differ!). I would be very grateful if someone could help shed some light.

Juniejunieta answered 5/5, 2017 at 8:23 Comment(5)
I'm also having this kind of problem, any solutions available?Beisel
Hi Riza, I asked the same questionover on the ASP.NET forums... and sadly no answer. Can you get the VC to render using @await Component.InvokeAsync("ComponentName") method? That works for me, but TagHelper is not working.Juniejunieta
yes, @await Component works, but TagHelper doesn't. I'm still trying to find the solution, will post an answer if I ever found oneBeisel
is there any dotnet frameworks update? unfortunately I'm using VSCodeBeisel
I am also having this issue, and cannot for the life of me figure out why it's not working. I've even built a sample of the code from the Microsoft documentation, and it still only works if I use the razor syntax.Branle
B
0

Installing .NET Core 1.1.2 runtime (Current LTS) solve my problem. I guess we should install both the SDK and the LTS runtime.

Beisel answered 27/5, 2017 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.