public IActionResult Index(InspectorViewModel inspectorAdd)
 {
     if (ModelState.IsValid)
     {
         using (var db = new InspectorDBContext())
         {
             db.Inspectors.Add(inspectorAdd.NewInspector);
             db.SaveChanges();
         }
     }
     return(RedirectToAction("Index"));
 }
        // GET: /<controller>/
        public IActionResult Index()
        {
            InspectorViewModel inspectorVM = new InspectorViewModel();

            using (var db = new InspectorDBContext())
            {
                inspectorVM.InspectorList = db.Inspectors.ToList();
                inspectorVM.NewInspector  = new Inspector();
            }

            return(View(inspectorVM));
        }
        //GET Filled in Form
        public IActionResult Edit(Guid id)
        {
            //a new instance of the view model
            InspectorViewModel inspVM = new InspectorViewModel();

            using (InspectorDBContext db = new InspectorDBContext())
            {
                inspVM.NewInspector = db.Inspectors.Where(
                    e => e.InspectorId == id).SingleOrDefault();

                //return view model
                return(View(inspVM));
            }
        }
 public IActionResult Edit(InspectorViewModel obj)
 {
     //check for valid model
     if (ModelState.IsValid)
     {
         using (InspectorDBContext db = new InspectorDBContext())
         {
             //object for view model
             Inspector i = obj.NewInspector;
             //retrieve primary key/id from route data
             i.InspectorId = Guid.Parse(RouteData.Values["id"].ToString());
             //update record status
             db.Entry(i).State = EntityState.Modified;
             db.SaveChanges();
         }
     }
     return(RedirectToAction("Index"));
 }
Example #5
0
        private static List <SelectListItem> GetInspectorsDDL()
        {
            List <SelectListItem> inspector = new List <SelectListItem>();
            InspectorViewModel    ivm       = new InspectorViewModel();

            using (var db = new InspectorDBContext())
            {
                ivm.InspectorList = db.Inspectors.ToList();
            }
            foreach (Inspector i in ivm.InspectorList)
            {
                inspector.Add(new SelectListItem
                {
                    Text  = i.InspectorFirst + " " + i.InspectorLast,
                    Value = i.InspectorId.ToString()
                });
            }
            return(inspector);
        }
Example #6
0
        public IActionResult Delete(Guid id)
        {
            InspectorViewModel inspectorVm = new InspectorViewModel();

            using (InspectorDBContext db = new InspectorDBContext())
            {
                using (var dbI = new InspectionDBContext())
                    using (var dbMR = new MaintenanceRecordDBContext())
                    {
                        InspectionViewModel inspectionVm = new InspectionViewModel();
                        inspectionVm.InspectionList = dbI.Inspections.ToList();
                        inspectionVm.NewInspection  = dbI.Inspections.Where(
                            i => i.InspectorId == id).FirstOrDefault();

                        MaintenanceRecordViewModel MRecordVm = new MaintenanceRecordViewModel();
                        MRecordVm.MaintenanceRecordList = dbMR.MaintenanceRecords.ToList();
                        MRecordVm.NewMaintenanceRecord  = dbMR.MaintenanceRecords.Where(
                            i => i.InspectorId == id).FirstOrDefault();


                        //create an instance of the view model
                        if (inspectionVm.NewInspection == null && MRecordVm.NewMaintenanceRecord == null)
                        {
                            inspectorVm.NewInspector = new Inspector();
                            //find id
                            inspectorVm.NewInspector.InspectorId =
                                Guid.Parse(RouteData.Values["id"].ToString());
                            //update record status
                            db.Entry(inspectorVm.NewInspector).State = EntityState.Deleted;
                            db.SaveChanges();
                            TempData["ResultMessage"] = "Inspector Deletion Successful";
                        }
                        else
                        {
                            TempData["ResultMessage"] = "Dependencies Found.  This Inspector cannot be deleted!";
                        }
                    }
            }
            return(RedirectToAction("Index"));
        }