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!