0

I need to change the property IsOpen to false from a SupportTicket with a button click in the ticket list and refresh the view so it only displays open tickets (IsOpen = true).

I tried with public IActionResult Resolve(int number) but doesn't work

HomeController.cs:

using Ev02.Models;
using Microsoft.AspNetCore.Mvc;

namespace Ev02.Controllers
{
    public class HomeController : Controller
    {
        private readonly IRepository _repository;

        public HomeController(IRepository repository)
        {
            this._repository = repository;
        }
        public IActionResult Index()
        {
            return View(this._repository.TicketList());
        }

        public IActionResult Save(SupportTicket ticket)
        {
            if(ModelState.IsValid)
            {
                this._repository.Add(ticket);
                ViewBag.Message = "Ticket guardado con éxito";
                return RedirectToAction(nameof(Index));
            }
            return View("Index", ticket);
        }

        public IActionResult Closed()
        {
            return View(this._repository.TicketList());
        }

        public IActionResult New()
        {
            SupportTicket ticket = new SupportTicket();
            return View(ticket);
        }

        public IActionResult Resolve(int number)
        {
            SupportTicket ticket = this._repository.TicketGetByNumber(number);
            ticket.IsOpen = false;
            return View(ticket);
        }
    }
}

Index.cshtml:

@{
    Layout = "_Layout";
}
@model List<Ev02.Models.SupportTicket>
<h2>Tickets pendientes de resolución</h2>
<div class="col-2">
    <table class="table">
        <thead>
            <tr>
                <th scope="col">N° de ticket</th>
                <th scope="col">Asunto</th>
                <th scope="col">Descripción</th>
                <th scope="col">Fecha de creación</th>
                <th scope="col">Acción</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var ticket in Model)
            if (ticket.IsOpen)
                {
                    <tr>
                        <td>@ticket.TicketNumber</td>
                        <td>@ticket.Subject</td>
                        <td>@ticket.Description</td>
                        <td>@ticket.CreatedDate</td>
                        @using (Html.BeginForm("Resolve", "Home", FormMethod.Post))
                        {
                            <td>
                                <button name="btn" type="submit" class="btn btn-light btn-sm">Marcar como resuelto</button>
                            </td>
                        }
                        

                    
                    </tr>
                }
            
        </tbody>
    </table>
</div>

I tried with public IActionResult Resolve(int number) but doesn't work

  • Based on what you've said and looking at your code, there could be multiple things causing the issue. What exactly isn't working? What happens instead of the desired behaviour? – Shahzad May 21 '23 at 00:19
  • Desired behaviour: `IsOpen` changes to `false` for the selected ticket, ticket list view refreshes and the modified ticket no longer appears on the list. Actual behaviour: Execution halts and VisualStudio throws this error. ```System.NullReferenceException: 'Object reference not set to an instance of an object.' ticket was null.``` – Alejandro Soto Zapata May 21 '23 at 00:43
  • Have you confirmed the `number` is being passed properly to `Resolve(int number)` Action method? Either it isn't coming through properly, or your repository isn't retrieving the ticket properly using the `number` – Shahzad May 21 '23 at 00:52
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Fildor May 21 '23 at 05:15

1 Answers1

0

It looks like you didn't save the changes only update the entity. For example, in EF, you need to invoke _youDbContext.SaveChanges() method after you update the entity.

So, you should invoke the Repository.Save() method after updating the flag and before returning the view.

P.S The model is IEnumerable and you're passing a single ticket.

SpicyCatGames
  • 863
  • 7
  • 25
HaimBi
  • 13
  • 4