public ViewResult Edit(int id) { Incident activeIncident = new Incident(); Technician activeTechnician = new Technician(); var model = new IncidentAddEditViewModel { ActiveIncident = activeIncident, ActiveTechnician = activeTechnician, Technicians = context.Technicians.ToList(), Customers = context.Customers.ToList(), Products = context.Products.ToList(), Incidents = context.Incidents.ToList(), Action = "Edit" }; IQueryable <Incident> query = context.Incidents; if (activeIncident.IncidentID != 0) { query = query.Where(i => i.IncidentID == activeIncident.IncidentID); } if (activeTechnician.TechnicianID != 0) { query = query.Where(t => t.Technician.TechnicianID == activeTechnician.TechnicianID); } model.Incidents = query.ToList(); model.ActiveIncident = context.Incidents.Find(id); return(View("AddEdit", model)); }
public IActionResult Edit(int id) { var technicianOptions = new QueryOptions <Technician>(); var customerOptions = new QueryOptions <Customer>(); var productOptions = new QueryOptions <Product>(); var incidentOptions = new QueryOptions <Incident> { Includes = "Customer,Product", WhereClauses = new WhereClauses <Incident> { { t => t.IncidentID == id } } }; // Create a new instance of the view model var viewModel = new IncidentAddEditViewModel(); // Populate List<> and properties of the view model viewModel.operationType = "Edit"; viewModel.CurrentIncident = data.Incidents.Get(id); viewModel.Technicians = data.Technicians.List(technicianOptions); viewModel.Customers = data.Customers.List(customerOptions); viewModel.Products = data.Products.List(productOptions); // Populate Incidents List<> with those having the selected Incident viewModel.Incidents = data.Incidents.List(incidentOptions); // Set session state for TechnicianID HttpContext.Session.SetInt32("sessionTechId", (int)viewModel.CurrentIncident.TechnicianID); return(View(viewModel)); }
public ViewResult Add() { Incident activeIncident = new Incident(); Technician activeTechnician = new Technician(); var model = new IncidentAddEditViewModel { ActiveIncident = activeIncident, ActiveTechnician = activeTechnician, Incidents = context.Incidents.ToList(), //not sure if necessary Technicians = context.Technicians.ToList(), Customers = context.Customers.ToList(), Products = context.Products.ToList(), Action = "Add" }; IQueryable <Incident> query = context.Incidents; if (activeIncident.IncidentID != 0) { query = query.Where(i => i.IncidentID == activeIncident.IncidentID); } if (activeTechnician.TechnicianID != 0) { query = query.Where(i => i.Technician.TechnicianID == activeTechnician.TechnicianID); } model.Incidents = query.ToList(); return(View("AddEdit", model)); }
public IActionResult Add() { // Create a view model for add IncidentAddEditViewModel views = new IncidentAddEditViewModel(); views.customers = sportsUnit.Customers.List(new QueryOptions <Customer>()); views.products = sportsUnit.Products.List(new QueryOptions <Product>()); views.technicians = sportsUnit.Technicians.List(new QueryOptions <Technician>()); views.operation = "Add"; views.currentIncident = new Incident(); return(View(views)); }
public IActionResult Edit(int id) { // Create a view model for editing Incident inc = sportsUnit.Incidents.Get(id); IncidentAddEditViewModel views = new IncidentAddEditViewModel(); views.customers = sportsUnit.Customers.List(new QueryOptions <Customer>()); views.products = sportsUnit.Products.List(new QueryOptions <Product>()); views.technicians = sportsUnit.Technicians.List(new QueryOptions <Technician>()); views.operation = "Edit"; views.currentIncident = inc; return(View("Add", views)); }
// Display incidents assigned to the selected technician public IActionResult List(int technicianId) { // Obtain session state for TechnicianID so website shows List page for that technician only // The session state is set in the Edit() action method int?sessionTechId = HttpContext.Session.GetInt32("sessionTechId"); // Check if there is a session state for TechnicianID (ie. user is already in session on our website) if (sessionTechId != null) { technicianId = (int)sessionTechId; } // No session state and the user selects a Technician if (technicianId != 0) { var productOptions = new QueryOptions <Product>(); var customerOptions = new QueryOptions <Customer>(); var incidentOptions = new QueryOptions <Incident> { Includes = "Customer,Product", WhereClauses = new WhereClauses <Incident> { { t => t.TechnicianID == technicianId }, { i => i.DateClosed == null } } }; var viewModel = new IncidentAddEditViewModel(); viewModel.Products = data.Products.List(productOptions); viewModel.Customers = data.Customers.List(customerOptions); viewModel.CurrentTechnician = data.Technicians.Get(technicianId); // Populate Incidents List<> with incident object meeting query above viewModel.Incidents = data.Incidents.List(incidentOptions); // Check if the selected Technician has any open incidents if (viewModel.Incidents.Count == 0) { TempData["message"] = $"No open incidents for this technician."; } return(View(viewModel)); } else { // User does not select a Technician TempData["message"] = "Please select a technician."; return(RedirectToAction("Get")); } }//List() action method
public IActionResult Add(IncidentAddEditViewModel views) { try { // update the db context with added model Incident incident = views.currentIncident; sportsUnit.Incidents.Insert(incident); sportsUnit.save(); TempData["message"] = $"{incident.Title} was successfully added"; return(RedirectToAction("List", "Incident")); } catch { return(View(views)); } }
public IActionResult Edit(IncidentAddEditViewModel viewModel) // To save revised data for the incident { if (ModelState.IsValid) { data.Incidents.Update(viewModel.CurrentIncident); data.Incidents.Save(); TempData["message"] = $"Incident updated successfully."; return(RedirectToAction("List", "TechIncident")); } else { viewModel.CurrentIncident = data.Incidents.Get(viewModel.CurrentIncident.IncidentID); viewModel.operationType = "Edit"; return(View("Edit", viewModel)); } }
public IActionResult Edit(int id) { var incidentOptions = new QueryOptions <Incident> { Includes = "Customer,Product" }; var technicianOptions = new QueryOptions <Technician>(); var customerOptions = new QueryOptions <Customer>(); var productOptions = new QueryOptions <Product>(); // Create a new instance of the view model and populate the necessary List<> and properties var viewModel = new IncidentAddEditViewModel(); viewModel.operationType = "Edit"; viewModel.Products = data.Products.List(productOptions); viewModel.Customers = data.Customers.List(customerOptions); viewModel.Technicians = data.Technicians.List(technicianOptions); viewModel.CurrentIncident = data.Incidents.Get(id); return(View("AddEdit", viewModel)); }
public IActionResult Edit(IncidentAddEditViewModel views, int?dest) { try { // update the db context with editted model Incident incident = views.currentIncident; sportsUnit.Incidents.Update(incident); sportsUnit.save(); TempData["message"] = $"{incident.Title} was successfully updated"; int?techID = http.HttpContext.Session.GetInt32("techID"); if (dest != null) { return(RedirectToAction("TechList", "Incident", new { TechnicianID = techID })); } return(RedirectToAction("List", "Incident")); } catch { return(View("Add", views)); } }
public IActionResult Add() { var incidentOptions = new QueryOptions <Incident> { Includes = "Customer,Product" }; var technicianOptions = new QueryOptions <Technician>(); var customerOptions = new QueryOptions <Customer>(); var productOptions = new QueryOptions <Product>(); // create a new instance of the view model var viewModel = new IncidentAddEditViewModel(); // Populate List<> and properties of the view model viewModel.operationType = "Add"; viewModel.Products = data.Products.List(productOptions); viewModel.Customers = data.Customers.List(customerOptions); viewModel.Technicians = data.Technicians.List(technicianOptions); viewModel.CurrentIncident = new Incident(); return(View("AddEdit", viewModel)); }
public IActionResult Edit(IncidentAddEditViewModel model) { if (ModelState.IsValid) { if (model.ActiveIncident.IncidentID == 0) { data.Incidents.Insert(model.ActiveIncident); } else { data.Incidents.Update(model.ActiveIncident); } data.Incidents.Save(); return(RedirectToAction("List", "Incident")); } else { ViewBag.Action = model.ActiveIncident.IncidentID == 0 ? "Add" : "Edit"; return(View("AddEdit", model)); } }
public ViewResult Add() { Incident activeIncident = new Incident(); Technician activeTechnician = new Technician(); var incidentOptions = new QueryOptions <Incident> { Includes = "Customer,Product" }; var technicianOptions = new QueryOptions <Technician>(); var customerOptions = new QueryOptions <Customer>(); var productOptions = new QueryOptions <Product>(); var model = new IncidentAddEditViewModel { ActiveIncident = activeIncident, ActiveTechnician = activeTechnician, Incidents = data.Incidents.List(incidentOptions), Technicians = data.Technicians.List(technicianOptions), Customers = data.Customers.List(customerOptions), Products = data.Products.List(productOptions), Action = "Add" }; return(View("AddEdit", model)); }
public ViewResult Edit(int id) { Incident activeIncident = context.Incidents.Find(id); var model = new IncidentAddEditViewModel { ActiveIncident = activeIncident, Incidents = context.Incidents.ToList(), Technicians = context.Technicians.ToList(), Customers = context.Customers.ToList(), Products = context.Products.ToList(), Action = "Edit" }; IQueryable <Incident> query = context.Incidents; if (activeIncident.IncidentID != 0) { query = query.Where(i => i.IncidentID == activeIncident.IncidentID); } model.Incidents = query.ToList(); model.ActiveIncident = context.Incidents.Find(id); HttpContext.Session.SetInt32("sessionID", (int)model.ActiveIncident.TechnicianID); return(View("Edit", model)); }
// Display technician names in a dropdown menu public IActionResult Get() { // Remove session state for TechnicianID so that website throws a message if user clicks "select" button without selecting a Technician HttpContext.Session.Remove("sessionTechId"); var incidentOptions = new QueryOptions <Incident> { Includes = "Customer,Product" }; var technicianOptions = new QueryOptions <Technician>(); var customerOptions = new QueryOptions <Customer>(); var productOptions = new QueryOptions <Product>(); // Create a new instance of the view model var viewModel = new IncidentAddEditViewModel(); // Popluate only the List<> and properties needed in the Views/Get.cshtml file viewModel.Incidents = data.Incidents.List(incidentOptions); viewModel.Technicians = data.Technicians.List(technicianOptions); viewModel.CurrentIncident = new Incident(); viewModel.CurrentTechnician = new Technician(); return(View(viewModel)); }
public IActionResult Edit(IncidentAddEditViewModel viewModel) { if (ModelState.IsValid) { if (viewModel.CurrentIncident.IncidentID == 0) { data.Incidents.Insert(viewModel.CurrentIncident); TempData["message"] = $"1 new incident has been added"; } else { data.Incidents.Update(viewModel.CurrentIncident); TempData["message"] = $"Incident ID: {viewModel.CurrentIncident.IncidentID} has been editted."; } data.Incidents.Save(); return(RedirectToAction("List", "Incident")); } else { // Validation on the form fails. Reload same page with viewmodel data already there viewModel.operationType = (viewModel.CurrentIncident.IncidentID == 0) ? "Add" : "Edit"; return(View("AddEdit", viewModel)); } }