using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ViewComponentsSample.Models;
namespace ViewComponentsSample.Views.Components
{
// View Component class
public class PriorityListViewComponent : ViewComponent
{
private readonly ToDoContext db;
public PriorityListViewComponent(ToDoContext context)
{
db = context;
}
// InvokeAsync - main entry point
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)
.ToListAsync();
}
}
}