0

I am trying to create my first View Component in my MVC project but have come to a halt. When I try to invoke my View Component from the razor view page I get an error that states: "The name 'await' does not exist in the current context".

Code I use to call the View Component:

@await Component.InvokeAsync("Example")

View Component class:

namespace MyWebApp.ViewComponents
{
    public class ExampleViewComponent : ViewComponent
    {
        public IViewComponentResult Invoke()
        {
            var user = Account.GetCurrent();

            return View(user);
        }
    }
}

View Component razor view file location: Views/Shared/Components/Example/Default.cshtml

View Component class file location: ViewComponents/ExampleViewComponent.cs

I have mostly been following this tutorial: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1

SOLVED : Check comments

4
  • Where are you calling the View component ? Is it inside a razor view file ? Commented Sep 17, 2018 at 19:31
  • yes, inside Views/Home/Index.cshtml Commented Sep 17, 2018 at 19:38
  • 1
    Which MVC version you're using? @await Component.InvokeAsync("Example") works only for Core MVC, in previous versions you need child actions (marked as ChildActionOnlyAttribute). Commented Sep 18, 2018 at 4:41
  • Turns out it's a asp.net application and not asp.net core. Thanks for the heads up. created a child action method now and works fine :) Commented Sep 18, 2018 at 14:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.