text
stringlengths
0
13.4k
Controllers/TodoController.cs
public async Task<IActionResult> Index()
{
var items = await _todoItemService.GetIncompleteItemsAsync();
var model = new TodoViewModel()
{
Items = items
};
return View(model);
}
If you haven't already, make sure these using statements are at the top
of the file:
using AspNetCoreTodo.Services;
using AspNetCoreTodo.Models;
If you're using Visual Studio or Visual Studio Code, the editor will suggest
these using statements when you put your cursor on a red squiggly
line.
Test it out
41
Finish the controller
To start the application, press F5 (if you're using Visual Studio or Visual
Studio Code), or just type dotnet run in the terminal. If the code
compiles without errors, the server will start up on port 5000 by default.
If your web browser didn't open automatically, open it and navigate to
http://localhost:5000/todo. You'll see the view you created, with the
data pulled from your fake database (for now).
Although it's possible to go directly to http://localhost:5000/todo , it
would be nicer to add an item called My to-dos to the navbar. To do this,
you can edit the shared layout file.
42
Update the layout
Update the layout
The layout file at Views/Shared/_Layout.cshtml contains the "base"
HTML for each view. This includes the navbar, which is rendered at the
top of each page.
To add a new item to the navbar, find the HTML code for the existing
navbar items:
Views/Shared/_Layout.cshtml
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">
Home
</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="About">
About
</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Contact">
Contact
</a></li>
</ul>
Add your own item that points to the Todo controller instead of Home :