nam
nam

Reputation: 23868

ASP.NET Core ViewComponent's Invoke() method call not recognized

In my ASP.NET Core RC2 app with VS2015 I have following Invoke() method in the ViewComponent class but intellisense is not recognizing the Invoke() method. The app builds successfully but when running it I get the following error:

System.Threading.Tasks.Task`1[Microsoft.AspNetCore.Html.IHtmlContent]

The Invoke() method in ViewComponent class:

public IViewComponentResult Invoke(string filter)
        {
            var res = (from p in ctx.Product.ToList()
                       where p.ProductName.StartsWith(filter)
                       select p).ToList();

            return View(res);
        }

The Invoke() method call from a view that generates the above error:

<div class="alert alert-success">@Component.Invoke("ProductList", "D")</div>

Upvotes: 4

Views: 6104

Answers (2)

Jason
Jason

Reputation: 311

This is an old question but I wanted to provide an answer for the 1.0 RTM version of ASP.NET Core.

@await Component.InvokeAsync("ProductList", new { filter = "D" })

Component.Invoke() has been removed. So, this is the right way to do it even if the method in the view component is still defined as Invoke() (and not InvokeAsync())—which might be alright depending on what the method does.

As an alternative, you could also use the more strongly-typed version...

@(await Component.InvokeAsync<ProductList>(new { filter = "D" }))

Upvotes: 9

Eric Liu
Eric Liu

Reputation: 643

  • What packages version are you using in the project?
  • What's the target framework?
  • What's the environtment?
  • What web server/listener are you using?
  • Check if the Component's view is correct, and have the correct model (at /Shared/Component/ComponentName/Some.cshtml)

Try use Async instead of sync component call (Actually I don't even have sync invocation)

public async Task<IViewComponentResult> InvokeAsync(string filter)
{
   var res = await (from p in ctx.Products
                       where p.ProductName.StartsWith(filter)
                       select p).ToListAsync();

            return View(res);
}

@await Component.InvokeAsync("ProductList", "D")

I don't actually know if this code works or even compile (I don't use EF)

If the exception is still the same, try update .net core to RTM (preview2-003...) And update the packages to the latest versions.

Here is the doc for View Components

Upvotes: 0

Related Questions