text
stringlengths 0
13.4k
|
|---|
Index action name can be left out of the URL. You can customize this
|
behavior if you'd like, but for now, we'll stick to the default conventions.
|
Add a new action called Index to the TodoController , replacing the //
|
Actions go here comment:
|
public class TodoController : Controller
|
{
|
public IActionResult Index()
|
{
|
// Get to-do items from database
|
// Put items into a model
|
// Render view using the model
|
}
|
}
|
Action methods can return views, JSON data, or HTTP status codes like
|
200 OK and 404 Not Found . The IActionResult return type gives you
|
the flexibility to return any of these from the action.
|
It's a best practice to keep controllers as lightweight as possible. In this
|
case, the controller will be responsible for getting the to-do items from
|
the database, putting those items into a model the view can understand,
|
and sending the view back to the user's browser.
|
Before you can write the rest of the controller code, you need to create a
|
model and a view.
|
23
|
Create models
|
Create models
|
There are two separate model classes that need to be created: a model
|
that represents a to-do item stored in the database (sometimes called an
|
entity), and the model that will be combined with a view (the MV in
|
MVC) and sent back to the user's browser. Because both of them can be
|
referred to as "models", I'll refer to the latter as a view model.
|
First, create a class called TodoItem in the Models directory:
|
Models/TodoItem.cs
|
using System;
|
using System.ComponentModel.DataAnnotations;
|
namespace AspNetCoreTodo.Models
|
{
|
public class TodoItem
|
{
|
public Guid Id { get; set; }
|
public bool IsDone { get; set; }
|
[Required]
|
public string Title { get; set; }
|
public DateTimeOffset? DueAt { get; set; }
|
}
|
}
|
This class defines what the database will need to store for each to-do
|
item: an ID, a title or name, whether the item is complete, and what the
|
due date is. Each line defines a property of the class:
|
24
|
Create models
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.