Models would propably look like this:
public class TodosCreateViewModel
{
[Required]
[MaxLength(400)]
public string Name { get; set; }
[Required]
public bool IsDone { get; set; }
[Required]
public DateTime CreatedOn { get; set; } = DateTime.Today;
}public class TodosEditViewModel
{
[HiddenInput]
public int Id { get; set; }
[Required]
[MaxLength(400)]
public string Name { get; set; }
[Required]
public bool IsDone { get; set; }
[Required]
public DateTime CreatedOn { get; set; } = DateTime.Today;
} public class TodosIndexViewModel
{
public List<TodoItem> TodoItems { get; set; }
}
HTML views
<div class="text-center">
<h1 class="display-4">Create Todo</h1>
</div>
<form method="post" class="d-flex flex-column gap-2">
<div>
<label class="form-label">@Html.DisplayNameFor(m => m.Name)</label>
<input asp-for="Name" class="form-control"/>
</div>
<div class="form-check">
<input asp-for="IsDone" class="form-check-input"/>
<label class="form-check-label">@Html.DisplayNameFor(m => m.IsDone)</label>
</div>
<div>
<label class="form-label">@Html.DisplayNameFor(m => m.CreatedOn)</label>
<input asp-for="CreatedOn" class="form-control"/>
</div>
<button class="btn btn-primary">Create</button>
</form><div class="text-center">
<h1 class="display-4">Edit Todo</h1>
</div>
<form method="post" class="d-flex flex-column gap-2">
<input asp-for="Id"/>
<div>
<label class="form-label">@Html.DisplayNameFor(m => m.Name)</label>
<input asp-for="Name" class="form-control"/>
</div>
<div class="form-check">
<input asp-for="IsDone" class="form-check-input"/>
<label class="form-check-label">@Html.DisplayNameFor(m => m.IsDone)</label>
</div>
<div>
<label class="form-label">@Html.DisplayNameFor(m => m.CreatedOn)</label>
<input asp-for="CreatedOn" class="form-control"/>
</div>
<button class="btn btn-primary">Edit</button>
</form><div class="text-center">
<h1 class="display-4">Todos</h1>
</div>
<table class="table table-striped table-dark">
<thead>
<tr>
<th>Todo Name</th>
<th>Is Done</th>
<th>Created On</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.TodoItems)
{
<tr>
<td>@item.Name</td>
<td>@(item.IsDone ? "Yes" : "No")</td>
<td>@item.CreatedOn</td>
<td>
<a class="btn btn-info" asp-controller="Todos" asp-action="Edit" asp-route-id="@item.Id">
Edit
</a>
<a class="btn btn-danger" asp-controller="Todos" asp-action="Delete" asp-route-id="@item.Id">
Delete
</a>
</td>
</tr>
}
</tbody>
</table>
<a asp-controller="Todos" asp-action="Create" class="btn btn-primary">Create</a>
And try to do this in program.cs it should work
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
}
}
}