0

I have details View and in details view as Admin I want to be able to update only Status of ticket nothing more. When I create function in controller which is something like this

public IActionResult UpdateStatus(int ticketId, string status)
        {
            var ticket = _unitOfwork.Ticket.Get(ticketId);
            ticket.Status = status;
            _unitOfwork.Ticket.Update(ticket);
            return View();
        }

I get error message

Object reference not set to an instance of an object.

And in my model I have something like this

 public class Ticket
    {
        [Key]
        public int Id { get; set; }

        [Display(Name = "Opis")]
        public string Description { get; set; }

        [Display(Name = "Datum i vrijeme slanja")]
        public string DateAndTime { get; set; } = DateTime.Now.ToString("dd/MM/yyyy HH:mm");

        [Display(Name = "Datum i vrijeme zavrsetka")]
        public DateTime Answered { get; set; }

        [Display(Name = "Vrsta tiketa")]
        public int TicketTypeID { get; set; }

        [ForeignKey("TicketTypeID")]
        public virtual TicketType TicketType { get; set; }

        public string UserId { get; set; }

        [ForeignKey("UserId")]
        public virtual ApplicationUser ApplicationUser { get; set; }

        public int? ClientId { get; set; }
        [ForeignKey("ClientId")]
        public virtual Client Client { get; set; }

        public string Status { get; set; } = TicketStatus.Otvoren.ToString();

And TicketStatus is ENUM value which I get from here

public enum TicketStatus
    {
        Otvoren = 1,
        NaCekanju = 2,
        Zatvoren = 3
    }

And in Details View I have this structure

@model VmSTicketing.Models.ViewModels.TicketVM
@using VmSTicketing.Utility
@{
    ViewData["Title"] = "Details";
    Layout = "~/Views/Shared/AdminLTE/_Layout.cshtml";
}

<h2 class="text-info">Pregled tiketa</h2>

<div class="p-4 border rounded">

    <div class="form-group row">
        <div class="col-2">
            <label>Vrsta tiketa</label>
        </div>
        <div class="col-5">
            <input asp-for="Ticket.TicketType.Name" disabled class="form-control" />
        </div>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label>Opis</label>
        </div>
        <div class="col-5">
            <textarea asp-for="Ticket.Description" disabled class="form-control"></textarea>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label>Datum i vrijeme slanja</label>
        </div>
        <div class="col-5">
            <input asp-for="Ticket.DateAndTime" disabled class="form-control" />
        </div>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label>Klijent</label>
        </div>
        <div class="col-5">
            <input asp-for="Client.Name" disabled class="form-control" />
        </div>
    </div>
    <div class="form-group row">
        <div class="col-2">
            <label>User</label>
        </div>
        <div class="col-5">
            <input asp-for="Ticket.ApplicationUser.Name" disabled class="form-control" />
        </div>
    </div>
    @if (User.IsInRole(SD.Role_Admin))
    {
        <div class="form-group row">
            <div class="col-2">
                <label>Status Tiketa</label>
            </div>
            <div class="col-5">
                <select asp-for="@Model.Ticket" asp-items="Html.GetEnumSelectList<VmSTicketing.Models.Enum.TicketStatus>()" class="form-control"></select>
            </div>
        </div>
        <div class="form-group">
            <a asp-area="Manager" asp-action="UpdateStatus"  asp-controller="Ticket" class="btn btn-primary">Update</a>
        </div>
    }

    <br />
    <div class="form-group">
        <a asp-action="Index" class="btn btn-success">Back to List</a>
    </div>

</div>

Where did I made mistake ? Why status is recognize as NULL object ?

10
  • var ticket = _unitOfwork.Ticket.Get(ticketId); ticket is null Commented Mar 12, 2021 at 13:19
  • "Why status is recognize as NULL object ?" - Did your debugging reveal that, or are you just assuming it? Because the error message you've indicated and the method which you indicate produces that error message in no way implies the conclusion you've made. You need to debug to find out what is null. It could be _unitOfwork, it could be _unitOfwork.Ticket, or it could be ticket. It's also equally possible that you've mistakenly identified the source of the error and it could be coming from somewhere else, such as from within the rendering of the view. Commented Mar 12, 2021 at 13:19
  • @Sergey Yes, exactly. var ticket is NULL Commented Mar 12, 2021 at 13:22
  • @David I debug and I notice that var ticket is NULL Commented Mar 12, 2021 at 13:22
  • 1
    Add a hidden field to your view with ticket.Id Commented Mar 12, 2021 at 13:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.