Hi,
I am in Rich Anderson's View Component Tutorial. However, when I ran the code, I got an
/Views/ToDo/Index.cshtml No overload for method 'InvokeAsync' takes 3 arguments 34. @await Component.InvokeAsync("PriorityList", 2, false)
Well, I follow it step by step, this is totally against what the tutorial says. It says the InvokeAsync function "can take an arbitrary number of arguments." What have happened? Here is my ViewComponent class
using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using StarterComponentView.Data; using StarterComponentView.Models; using Microsoft.AspNetCore.Mvc; namespace StarterComponentView.ViewComponents { [ViewComponent(Name = "PriorityList")] public class PriorityListViewComponent: ViewComponent { private readonly ApplicationDbContext db; public PriorityListViewComponent(ApplicationDbContext context) { db = context; } public async Task<IViewComponentResult> InvokeAsync(int maxPriority, bool isDone) { var items = await GetItemsAsync(maxPriority, isDone); return View(items); } private Task<List<ToDoItem>> GetItemsAsync(int maxPriority, bool isDone) { return db.ToDo.Where(x => x.IsDone == isDone && x.Priority <= maxPriority).ToAsyncEnumerable().ToList(); //.ToListAsync(); } } }
Here is the calling View
@using StarterComponentView.Models @model IEnumerable<ToDoItem><h2>ToDo Starter</h2><table class="table"><tr><th> @Html.DisplayNameFor(model => model.IsDone)</th><th> @Html.DisplayNameFor(model => model.Priority)</th><th> @Html.DisplayNameFor(model => model.Name)</th><th></th></tr> @foreach (var item in Model) {<tr><td> @Html.DisplayFor(modelItem => item.IsDone)</td><td> @Html.DisplayFor(modelItem => item.Priority)</td><td> @Html.DisplayFor(modelItem => item.Name)</td></tr> }</table><div> @await Component.InvokeAsync("PriorityList", 2, false)</div>
And here is the ViewComponent's view section
@model IEnumerable<StarterComponentView.Models.ToDoItem><h3>Priority Items</h3><ul> @foreach (var todo in Model) {<li>@todo.Name</li> }</ul>
I hate it when myself just copy / paste and the tutorial code doesn't work