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 ?

UnKnowUser
  • 133
  • 10
  • var ticket = _unitOfwork.Ticket.Get(ticketId); ticket is null – Serge Mar 12 '21 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. – David Mar 12 '21 at 13:19
  • @Sergey Yes, exactly. `var ticket` is NULL – UnKnowUser Mar 12 '21 at 13:22
  • @David I debug and I notice that `var ticket` is NULL – UnKnowUser Mar 12 '21 at 13:22
  • 1
    Add a hidden field to your view with ticket.Id – Serge Mar 12 '21 at 13:23
  • @UnKnowUser: Then you've found the problem. You're trying to set a property on an object that isn't there. – David Mar 12 '21 at 13:24
  • @Sergey Doesn't work adding hidden field – UnKnowUser Mar 12 '21 at 13:27
  • @David How to solve this issues ? – UnKnowUser Mar 12 '21 at 13:37
  • @UnKnowUser: Ultimately you need to continue to debug to find the source of the issue. Why is `ticket` `null`? Because `_unitOfwork.Ticket.Get(ticketId)` returned `null`. Why did it return `null`? Debug to find out. Is `ticketId` the value you expect? Why or why not? What value do you expect it to be and where is that value coming from? Is your link with `asp-action="UpdateStatus"` passing any values at all to the controller? If not, how do you add the value(s) you want? Continue working the problem backwards from the exception until you find what you're missing. – David Mar 12 '21 at 13:39
  • I expect only status to be update from ticketId. If I have list of ticket,I want to be able to update ticketStatus only – UnKnowUser Mar 12 '21 at 13:43

0 Answers0