I have a form that I am pulling the data from a database. On this form, there are some items that will be set when the form is created and will never change. There are others that will be set when the form is created and then can be changed later. Finally, there are others that will not be set when the form is created but can be changed later. Some of these fields are empty text boxes, others are drop down lists. In my view, how do I display those fields that have already been filled out, but still allow them to be adjusted? Here is what I have as of now:
View:
<div> @using (Html.BeginForm("TicketEditor", "Home")) {<div class="lightbox" id="example1"><figure class="lightbox__body"><a href="#" class="lightbox__close-btn"></a><figcaption class="lightbox__content"><div>Ticket #: @Html.LabelFor(m => m.Id)</div><div>Requested By: @Html.LabelFor(n => n.RequestByName)</div><div>Serial # (Facility # if unavailable): @Html.TextBoxFor(m => m.SerialNum)</div><div>Priority: @Html.DropDownListFor(m => m.Priorities, new SelectList(Model.Priorities, "Id", "PriorityType"))</div><div>TicketType: @Html.DropDownListFor(m => m.TicketTypes, new SelectList(Model.TicketTypes, "Id", "Type"))</div><div>Condition: @Html.DropDownListFor(m => m.Conditions, new SelectList(Model.Conditions, "Id", "Condition1"))</div><div>Desription:<div> @Html.TextAreaFor(m => m.Description, 10, 100, null)</div></div><a href="#" onClick="history.go(-1);return false;">Return to Ticket View</a></figcaption></figure></div> }</div>
View Model:
public int Id { get; set; } public int? AssociatedAssetId { get; set; } public Guid RequestedById { get; set; } public string RequestByName { get; set; } public int LocationId { get; set; } public string LocationName { get; set; } public int DepartmentId { get; set; } public string DepartmentName { get; set; } public int? AreaId { get; set; } public List<Condition> Conditions { get; set; } public int? StatusId { get; set; } public List<TicketType> TicketTypes { get; set; } public List<Priority> Priorities { get; set; } public string Description { get; set; } public string SerialNum { get; set; } public DateTime CreateDate { get; set; } public List<Status> Statuses { get; set; } public Guid? AssignToId { get; set; } public string AssignToName { get; set; } public DateTime? AssignedDate { get; set; } public DateTime? ClosedDate { get; set; } }
Controller: (this is what I have for now, but I believe I still have some work to do here)
public IActionResult TicketEditor(int ticketId) { //create view model ticket object and populate it with data from database TicketRequestViewModel ticket = new TicketRequestViewModel(); var context = new MaintenanceContext(); var dbTicket = context.Ticket.FirstOrDefault(t => t.Id == ticketId); ticket.MapIn(dbTicket); return View(ticket); } [HttpPost] public IActionResult TicketEditor(TicketRequestViewModel ticket) { var context = new MaintenanceContext(); var dbTicket = context.Ticket.FirstOrDefault(t => t.Id == ticket.Id); ticket.MapOut(dbTicket); var result = context.SaveChanges(); if (result < 1) { //verify the update succeeded } return RedirectToAction("Index", "Home"); }