5.4 KiB
title | author | description | monikerRange | ms.author | ms.custom | ms.date | no-loc | uid | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Build a Blazor todo list app | guardrex | Build a Blazor app step-by-step. | >= aspnetcore-3.0 | riande | mvc | 07/30/2020 |
|
tutorials/build-a-blazor-app |
Build a Blazor todo list app
By Daniel Roth and Luke Latham
This tutorial shows you how to build and modify a Blazor app. You learn how to:
[!div class="checklist"]
- Create a todo list Blazor app project
- Modify Razor components
- Use event handling and data binding in components
- Use routing in a Blazor app
At the end of this tutorial, you'll have a working todo list app.
Prerequisites
Create a todo list Blazor app
-
Create a new Blazor app named
TodoList
in a command shell:dotnet new blazorserver -o TodoList
The preceding command creates a folder named
TodoList
to hold the app. Change directories to theTodoList
folder with the following command:cd TodoList
-
Add a new
Todo
Razor component to the app in thePages
folder using the following command:dotnet new razorcomponent -n Todo -o Pages
[!IMPORTANT] Razor component file names require a capitalized first letter, so confirm that the
Todo
component file name starts with a capital letterT
. -
In
Pages/Todo.razor
provide the initial markup for the component:@page "/todo" <h3>Todo</h3>
-
Add the
Todo
component to the navigation bar.The
NavMenu
component (Shared/NavMenu.razor
) is used in the app's layout. Layouts are components that allow you to avoid duplication of content in the app.Add a
<NavLink>
element for theTodo
component by adding the following list item markup below the existing list items in theShared/NavMenu.razor
file:<li class="nav-item px-3"> <NavLink class="nav-link" href="todo"> <span class="oi oi-list-rich" aria-hidden="true"></span> Todo </NavLink> </li>
-
Rebuild and run the app. Visit the new Todo page to confirm that the link to the
Todo
component works. -
Add a
TodoItem.cs
file to the root of the project to hold a class that represents a todo item. Use the following C# code for theTodoItem
class: -
Return to the
Todo
component (Pages/Todo.razor
):- Add a field for the todo items in an
@code
block. TheTodo
component uses this field to maintain the state of the todo list. - Add unordered list markup and a
foreach
loop to render each todo item as a list item (<li>
).
- Add a field for the todo items in an
-
The app requires UI elements for adding todo items to the list. Add a text input (
<input>
) and a button (<button>
) below the unordered list (<ul>...</ul>
): -
Rebuild and run the app. When the
Add todo
button is selected, nothing happens because an event handler isn't wired up to the button. -
Add an
AddTodo
method to theTodo
component and register it for button selections using the@onclick
attribute. TheAddTodo
C# method is called when the button is selected: -
To get the title of the new todo item, add a
newTodo
string field at the top of the@code
block and bind it to the value of the text input using thebind
attribute in the<input>
element:<input placeholder="Something todo" @bind="newTodo" />
-
Update the
AddTodo
method to add theTodoItem
with the specified title to the list. Clear the value of the text input by settingnewTodo
to an empty string: -
Rebuild and run the app. Add some todo items to the todo list to test the new code.
-
The title text for each todo item can be made editable, and a check box can help the user keep track of completed items. Add a check box input for each todo item and bind its value to the
IsDone
property. Change@todo.Title
to an<input>
element bound to@todo.Title
: -
To verify that these values are bound, update the
<h3>
header to show a count of the number of todo items that aren't complete (IsDone
isfalse
).<h3>Todo (@todos.Count(todo => !todo.IsDone))</h3>
-
The completed
Todo
component (Pages/Todo.razor
): -
Rebuild and run the app. Add todo items to test the new code.
Next steps
In this tutorial, you learned how to:
[!div class="checklist"]
- Create a todo list Blazor app project
- Modify Razor components
- Use event handling and data binding in components
- Use routing in a Blazor app
Learn about tooling for ASP.NET Core Blazor:
[!div class="nextstepaction"] xref:blazor/tooling