Search code examples
c#asp.net-mvcdatabasedatetimeticket-system

Is there a way to make a DateTime property stay constant, as you edit its object with an MVC?


I am not sure how to go about this. My ticket object, has a DateTime property called TicketCreationTime, which said property is defaulting to 1/1/0001 at 12 AM; every time it's edit entity function is called from the tickets controller.

Image showing the tickets time defaulting to the beginning of time, in its view

How could I make it, so that the TicketCreationTime stays the same as changes are made to it's ticket object? That way, the creation time is always the time as when the ticket was made?

Ticket Object

public class Ticket
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime TicketCreationTime { get; set; } 
    public DateTime DueDate { get; set; }
    public DateTime TicketUpdatedTime { get; set; } 

    public Ticket()
    {
    }
}

TicketsController Create

public async Task<IActionResult> Create([Bind("ID,Name,Description,TicketCreationTime,DueDate,TicketUpdatedTime,CurrentStatus,Importance")] Ticket ticket)
{
    if (ModelState.IsValid)
    {
        DateTime StartTime = DateTime.Now;

        ticket.TicketCreationTime = StartTime;
        ticket.TicketUpdatedTime = StartTime;
        _context.Add(ticket);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }
    return View(ticket);
}

TicketsController Edit

public async Task<IActionResult> Edit(int id, [Bind("ID,Name,Description,TicketCreationTime,DueDate,TicketUpdatedTime,CurrentStatus,Importance")] Ticket ticket)
{
    if (id != ticket.ID)
    {
        return NotFound();
    }

    if (ModelState.IsValid)
    {
        try
        {
            ticket.TicketUpdatedTime = DateTime.Now;
            _context.Update(ticket);
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!TicketExists(ticket.ID))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return RedirectToAction(nameof(Index));
    }
    return View(ticket);
}

Solution

  • This problem is exactly why applying the Model concept is useful, which you may also recognize as the "M" in MVC.

    A Model class/object can look like an EF entity class/object, but it exists separately from actual database content.

    A Model can be used to display data (you may not always want to show all data, or you may want to show it in some other way from how it is stored) and/or a Model can be used to process data that is received from the rendered web page.

    In this case the Model class would have most of the properties from the Entity class, except for the Ticket....Time properties. When the Edit POST command is received, you need to retrieve the Entity object from the DbContext, update it with properties from the Model object, and then save the Entity object.

    Example code:

    public class TicketUpdateModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime DueDate { get; set; }
    }
    
    [HttpPost]
    public async Task<IActionResult> Edit(TicketUpdateModel model)
    {
        var ticket = await _context.Tickets.FindAsync(model.ID);
        if (ModelState.IsValid)
        {
            try
            {
                ticket.TicketUpdatedTime = DateTime.Now;
                ticket.Name = model.Name;
                ticket.Description = model.Description;
                ticket.DueDate = model.DueDate;
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                // ....
            }
            return RedirectToAction(nameof(Index));
        }
        return View(ticket); // or perhaps use a TicketDisplayModel class
    }