Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 222
nam Avatar asked Aug 30 '25 18:08

nam


1 Answers

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" }))
like image 91
Jason Avatar answered Sep 02 '25 08:09

Jason