Pregunta

I want the StartDate to remain the same date that was initially set when the ticket was first created.

I have removed the fields from the /Ticket/Edit view and have tried to set the StartDate property in the controller so it enters it automatically for the user.

However, I cannot seem to get it to work...

Here is my Ticket model:

public class Ticket
{
    [DisplayName("Ticket No.")]
    public int TicketID { get; set; }
    public int HardwareID { get; set; }
    public int StatusID { get; set; }
    public int PriorityID { get; set; }

    [StringLength(100)]
    [DisplayName("Summary")]
    public string Summary { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    [DisplayName("Date Created")]
    public DateTime StartDate { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    [DisplayName("Due Date")]
    public DateTime DueDate { get; set; }

    [DisplayName("Hardware Delivered?")]
    public Boolean HardwareDelivered { get; set; }

    public virtual Employee Employee { get; set; }
    public virtual Hardware Hardware { get; set; }
    public virtual Status Status { get; set; }
    public virtual Priority Priority { get; set; }
    public virtual AssignTicket AssignTicket { get; set; }
}

Here is my /Ticket/View Controller (GET and POST) which includes my attempt at setting the StartDate to the existing StartDate that is set for that particular ticket:

//
    // GET: /Ticket/Edit/5

    public ActionResult Edit(int id = 0)
    {
        Ticket ticket = db.Tickets.Find(id);
        if (ticket == null)
        {
            return HttpNotFound();
        }
        ViewBag.HardwareID = new SelectList(db.Hardwares, "HardwareID", "SerialNumber", ticket.HardwareID);
        ViewBag.StatusID = new SelectList(db.Statuses, "StatusID", "StatusName", ticket.StatusID);
        ViewBag.PriorityID = new SelectList(db.Priorities, "PriorityID", "PriorityName", ticket.PriorityID);
        return View(ticket);
    }

    //
    // POST: /Ticket/Edit/5

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Ticket ticket)
    {
        if (ModelState.IsValid)
        {
            ticket.StartDate = ticket.StartDate;
            db.Entry(ticket).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.HardwareID = new SelectList(db.Hardwares, "HardwareID", "SerialNumber", ticket.HardwareID);
        ViewBag.StatusID = new SelectList(db.Statuses, "StatusID", "StatusName", ticket.StatusID);
        ViewBag.PriorityID = new SelectList(db.Priorities, "PriorityID", "PriorityName", ticket.PriorityID);
        return View(ticket);
    }

Any help would be appreciated!

¿Fue útil?

Solución

your set in the post is just setting the date to itself. If you want to maintain the StartDate through the process you need to put it in a for helper on the view (except display) and it will post back to the controller

@Html.HiddenFor(x => x.StartDate)  // if you don't want to display it or use the display for to show it

using this you won't need to set it to itself. The value form the get will come back to the post

Otros consejos

ticket.StartDate = ticket.StartDate;

What exactly are you doing here? This does absolutely nothing you're overwriting the value of the variable with the value of the same variable?

What exactly happens when you save your ticket upon edit? Does it auto update to the latest date? How does that work. you must be writing DateTime.Now somewhere right?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top