Asp.net mvc compiler error when trying to invoke View Component from view page

让人想犯罪 __ 提交于 2021-02-11 12:03:17

问题


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://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1

SOLVED : Check comments

来源:https://stackoverflow.com/questions/52373631/asp-net-mvc-compiler-error-when-trying-to-invoke-view-component-from-view-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!