Activity 4 - Adding a new Razor page

Instructions

Create new page

  1. Ensure that you have Visual Studio Code open and in the Activity bar, Explorer is selected.

  2. In the Solution Explorer, next to the Pages directory, select the Add new file… button.

Screen shot of Add new file button

This will bring up a list of file type.

  1. Select Razor component from the list.
Screen shot of command pallette for create new file
  1. Name it todo and hit Enter.

    This will create the file and open it in the editor.

  2. Next, you will update the page to add a @page directive so that the page responds to /todo and add a page title using the <PageTitle> tag.

  1. Locate the navigation menu page (NavMenu.razor) under Shared and open it.

  2. Add a navigation item for your todo list above Fetch Data by copying the Fetch Data <div> and changing where it says FetchData or Fetch Data to Todo

  3. In the Solution Explorer, right click on your project name, select Debug and select Start new instance to see the changes view your To Do List.

Screenshot of To Do List added to the page

Create the To Do List

  1. Create a new Class file called TodoItem.cs to hold a class that represents the todo object.

At the top level of your project, click the Add new file… button and select Class from the list. Give it the name TodoItem.

  1. Add the following code to give a TodoItem a Title that is a string and has a property called IsDone that is a boolean.
public class TodoItem
{
    public string? Title { get; set; }
    public bool IsDone { get; set; }
}
  1. Go back to the todo.razor file and:
  1. When you click the Add todo button, nothing happens at this point. Now you’ll need to add an event handler.
  1. Update the list so that the titles are editable (<input>) and add a checkbox to keep track of completed items. Make sure you @bind the checkbox to the IsDone property of the todo.

  2. Update the <h3> to show the count of the number of todos that aren’t complete.

  3. Save and Run a new instance of your app so test it out.

Screenshot of completed app

You did it!
Workshop complete