FIX ASP.net controller

Max 0 Reputation points
2025-04-28T19:03:11.79+00:00

public class TodosController : Controller

{

 public AppDbContext DbContext { get; set; }

 public TodosController()

 {

     DbContext = new AppDbContext();

 }

 public IActionResult Index()

 {

     TodosIndexViewModel model = new TodosIndexViewModel();

     model.TodoItems = DbContext.TodoItems.ToList();

     return View(model);

 }

 public IActionResult Create()

 {

     TodosCreateViewModel model = new TodosCreateViewModel();

     return View(model);

 }

 [HttpPost]

 public IActionResult Create(TodosCreateViewModel model)

 {

     TodoItem entity = new TodoItem();

     entity.Name = model.Name;

     entity.IsDone = model.IsDone;

     entity.CreatedOn = model.CreatedOn;

     DbContext.TodoItems.Add(entity);

     DbContext.SaveChanges();

     return RedirectToAction(nameof(Index));

 }

 public IActionResult Edit(int id)

 {

     TodoItem? entity = DbContext.TodoItems.FirstOrDefault(x => x.Id == id);

     if (entity == null)

     {

         return NotFound();

     }

     TodosEditViewModel model = new TodosEditViewModel();

     model.Id = entity.Id;

     model.Name = entity.Name;

     model.CreatedOn = entity.CreatedOn;

     model.IsDone = entity.IsDone;

     return View(model);

 }

 [HttpPost]

 public IActionResult Edit(int id, TodosEditViewModel model)

 {

     TodoItem? entity = DbContext.TodoItems.FirstOrDefault(x => x.Id == id);

     if (entity == null)

     {

         return NotFound();

     }

     entity.Name = model.Name;

     entity.IsDone = model.IsDone;

     entity.CreatedOn = model.CreatedOn;

     //DbContext.TodoItems.Update(entity);

     DbContext.SaveChanges();

     return RedirectToAction(nameof(Index));

 }

 public IActionResult Delete(int id)

 {

     TodoItem? todoItem = DbContext.TodoItems.FirstOrDefault(x => x.Id == id);

     if (todoItem == null)

     {

         return NotFound();

     }

     DbContext.TodoItems.Remove(todoItem);

     DbContext.SaveChanges();

     return RedirectToAction(nameof(Index));

 }

}

ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
420 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Samir ⚽⚽⚽ 0 Reputation points
    2025-04-28T19:27:16.56+00:00

    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();
    
        }
    
    }
    

    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.