The name 'Component' does not exist in the current context

Asked

Viewed 1,913 times

2

I’m trying to create a Component view to reuse on the pages, I’m trying to follow some video tutorials. I’ve installed via nuget the entire Microsoft.AspNetCore.Mvc, but when I try to call it from the viewpage, using

@Component.InvokeAsync("Componente")

I have the error

The name 'Component' does not exist in the current context

inserir a descrição da imagem aqui

I’m using VS2017, Asp.net mvc 5. Does anyone know what might be missing? Install something else I don’t know? Or in mvc 5 is otherwise the Component call?

When I start typing Compon and pressing Ctrl+space should appear the list of options including the Component as shown in the following image, note that Component is not appearing:

inserir a descrição da imagem aqui

  • What version of Asp.net? If I’m not mistaken, ViewComponent is available from mvc core 1.1

  • dotnet --version 2.1.4

1 answer

3


In your case the problem is why View Components is an appeal lodged at MVC 6 and Asp.Net 5 and its application is actually a MVC5 with Asp.Net 4

Below is an example of the implementation in Dotenetcore (reference).

First create a directory, at the root of your MVC application, called Componentsview and add a new class called ComponenteViewComponent that must inherit from ViewComponent

public class ComponenteViewComponent : ViewComponent
{

    public async Task<IViewComponentResult> InvokeAsync()
    {
        return View();
    }
}

If you do not respect the suffix Viewcomponent the MVC will not recognize it as such and maybe there may be your first problem. If you created the class by calling only Componente, you need to add an annotation so that the component is recognized by that name.

[ViewComponent(Name = "Componente")]
public class Componente : ViewComponent
{

    public async Task<IViewComponentResult> InvokeAsync()
    {
        return View();
    }
}

Note the points above and after creating the class, go to the directory /Views/Shared/, add the directory Components and inside it another folder called Component, which is the name of its component.

Inside the folder Component add to PartialView calling for Default, my example I just added the following html snippet:

<h1>Olá eu sou um componente</h1>

Finally its structure must be like this:

inserir a descrição da imagem aqui

Now, in any view of your site you can invoke your component, in my case I added the snippet at the top of Homecontroller’s Index view:

<div>
    @await Component.InvokeAsync("Componente")
</div>

And that’s the result:

inserir a descrição da imagem aqui

  • 1

    Very interesting!

  • 1

    It ends up working as an "MVC within MVC", allows reuse of many components with greater malleability and decoupling than with simple partials views

  • @Leandroangelo remade exactly following the above steps, but the still problem, the problem is still in the component call in the view. now with await appears the same image error that was attached in the question, now being: The name 'await' does not exist in the current context. will I not have to install something by nuget that I’m not aware of? I installed this: Microsoft.AspNetCore.Mvc

  • How is your _Viewimports.cshtml in the root of the views folder

  • At the root of the Views folder files have only _Viewstart.cshtml and Web.config, _Viewstart.cshtml has the following content:@{ Layout = "~/Views/Shared/_Layout.cshtml"; }

  • When I start typing Compon and pressing Ctrl+space should appear the list of options including the Component as the image attached in the question now little note that Component is not appearing.

  • Web.Config??? you created the project as?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.