Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this exercise, you use everything you learned so far to add a basic to-do list page to your Blazor app.
Create a new Blazor Web App project.
Add a Todo.razor file to the Components/Pages folder
In Visual Studio and Visual Studio Code you can add the Razor file by right-clicking the Components/Pages folder in the Solution Explorer and selecting the appropriate option to add a new file.
You can also create the Razor file using the .NET CLI with this command:
dotnet new razorcomponent -n Todo -o Components/Pages
The -n|--name
option in the preceding command specifies the name of the new Razor component. The new component is created in the project's Components/Pages
folder with the -o|--output
option.
Important
Razor component file names should have a capitalized first letter so they can easily be distinguished from other HTML elements.
Open the Todo
component and add an @page
Razor directive to the top of the file with a relative URL of /todo
, and set the render mode to InteractiveServer
so the component can handle UI events.
@page "/todo"
@rendermode InteractiveServer
<h3>Todo</h3>
@code {
}
Apply the changes to the app and try browsing to "/todo" to see your new page.
The new to-do list page doesn't yet appear in the nav menu with the other existing pages. The nav menu is defined in the NavMenu
component, which is part of the app's layout. Let's update the NavMenu
component to add a link to the to-do list page.
Open Components/Layout/NavMenu.razor.
Find the nav
element in the NavMenu
component and add the following div
element below the existing nav item for the weather page.
<div class="nav-item px-3">
<NavLink class="nav-link" href="todo">
<span class="bi bi-list-nested-nav-menu" aria-hidden="true"></span> Todo
</NavLink>
</div>
The NavLink
component is a built-in Blazor component that renders an anchor tag. If the current browser address matches the href
for the NavLink
, it also renders an active
CSS class that you can use for styling the link.
After applying this change, you should now see the Todo page show up in the nav menu.
Create a new TodoItem.cs
file at the root of the project (the same level as Program.cs
) and add the following C# class to it.
public class TodoItem
{
public string? Title { get; set; }
public bool IsDone { get; set; } = false;
}
In Todo.razor add a field for the list of to-do items in the @code
block.
@code {
private List<TodoItem> todos = new();
}
Render an unordered list of all the to-dos using a foreach
loop.
<ul>
@foreach (var todo in todos)
{
<li>@todo.Title</li>
}
</ul>
You don't see anything rendered for the to-do list yet, because the list is empty. You need a way to add some to-do items.
Let's add some UI elements for adding to-do items to our list.
In Todo.razor add an input
tag and a button
below the unordered list.
<input />
<button>Add todo</button>
Create a newTodo
string field and bind it to the input
using the @bind
directive attribute.
<input @bind="newTodo" />
<button>Add todo</button>
@code {
private List<TodoItem> todos = new();
string newTodo = "";
}
Add a @onclick
handler to the button
that adds a new TodoItem
to the todos
list based on the value of newTodo
and then resets the value of newTodo
to an empty string.
<input @bind="newTodo" />
<button @onclick="AddTodo">Add todo</button>
@code {
private List<TodoItem> todos = new();
string newTodo = "";
void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo });
newTodo = string.Empty;
}
}
}
Check that you can now add to-do items and they show up in the list. The input
value should also reset after adding each to-do item.
You need a way to mark to-do items as completed, to edit existing to-do items, and to count how many to-do items are yet to be done.
Update the contents of the li
element to render an input
of type checkbox
that is bound to todo.IsDone
and a text input
that is bound to todo.Title
.
<ul>
@foreach (var todo in todos)
{
<li>
<input type="checkbox" @bind="todo.IsDone" />
<input @bind="todo.Title" />
</li>
}
</ul>
Update the <h3>
header to show a count of the number of todo items that aren't complete (IsDone
is false
).
<h3>Todo (@todos.Count(todo => !todo.IsDone))</h3>
When you finish adding code, your Todo.razor file should resemble the following:
@page "/todo"
@rendermode InteractiveServer
<h3>Todo (@todos.Count(todo => !todo.IsDone))</h3>
<ul>
@foreach (var todo in todos)
{
<li>
<input type="checkbox" @bind="todo.IsDone" />
<input @bind="todo.Title" />
</li>
}
</ul>
<input @bind="newTodo" />
<button @onclick="AddTodo">Add todo</button>
@code {
private List<TodoItem> todos = new();
string newTodo = "";
void AddTodo()
{
if (!string.IsNullOrWhiteSpace(newTodo))
{
todos.Add(new TodoItem { Title = newTodo });
newTodo = string.Empty;
}
}
}
After applying the changes to the app, try adding items, editing items, and marking todo items done to test the component.
Your Blazor to-do list is now done ✅.
Having an issue? We can help!
Please sign in to use this experience.
Sign in