// GET: Employee
        public ActionResult Index(int?id, int?ContractID)
        {
            var viewModel = new EmployeeIndexData();

            viewModel.Employees = db.Employees
                                  .Include(i => i.Contracts.Select(c => c.Job))
                                  .OrderBy(i => i.ID);

            if (id != null)
            {
                ViewBag.EmployeeID  = id.Value;
                viewModel.Contracts = viewModel.Employees.Where(
                    i => i.ID == id.Value).Single().Contracts;
            }

            if (ContractID != null)
            {
                ViewBag.ContractID = ContractID.Value;

                //commented out next three lines because doing using explicit loading
                // viewModel.Allocations = viewModel.Contracts.Where(
                //   x => x.ContractID == ContractID).Single().Allocations;
                //explicit loading
                var selectedContract = viewModel.Contracts.Where(x => x.ContractID == ContractID).Single();
                db.Entry(selectedContract).Collection(x => x.Allocations).Load();
                foreach (Allocation allocation in selectedContract.Allocations)
                {
                    db.Entry(allocation).Reference(x => x.Project).Load();
                }

                viewModel.Allocations = selectedContract.Allocations;
            }

            return(View(viewModel));
        }
Example #2
0
        public async Task <IActionResult> PutProduct(int id, Models.Product product)
        {
            if (id != product.ProductId)
            {
                return(BadRequest());
            }

            _context.Entry(product).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #3
0
        public async Task <IActionResult> PutOrder(int id, Models.Order order)
        {
            if (id != order.OrderId)
            {
                return(BadRequest());
            }

            _context.Entry(order).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult> Edit(int?id, byte[] rowVersion)
        {
            string[] fieldsToBind = new string[] { "Name", "EmployeeID", "RowVersion" };

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var jobToUpdate = await db.Jobs.FindAsync(id);

            if (jobToUpdate == null)
            {
                Job deletedJob = new Job();
                TryUpdateModel(deletedJob, fieldsToBind);
                ModelState.AddModelError(string.Empty,
                                         "Unable to save changes. The department was deleted by another user.");
                ViewBag.EmployeeID = new SelectList(db.Employees, "ID", "FirstName", deletedJob.EmployeeID);
                return(View(deletedJob));
            }

            if (TryUpdateModel(jobToUpdate, fieldsToBind))
            {
                try
                {
                    db.Entry(jobToUpdate).OriginalValues["RowVersion"] = rowVersion;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    var entry         = ex.Entries.Single();
                    var clientValues  = (Job)entry.Entity;
                    var databaseEntry = entry.GetDatabaseValues();
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty,
                                                 "Unable to save changes. The department was deleted by another user.");
                    }
                    else
                    {
                        var databaseValues = (Job)databaseEntry.ToObject();

                        if (databaseValues.Name != clientValues.Name)
                        {
                            ModelState.AddModelError("Name", "Current value: "
                                                     + databaseValues.Name);
                        }

                        if (databaseValues.EmployeeID != clientValues.EmployeeID)
                        {
                            ModelState.AddModelError("InstructorID", "Current value: "
                                                     + db.Employees.Find(databaseValues.EmployeeID).FirstName);
                        }
                        ModelState.AddModelError(string.Empty, "The record you attempted to edit "
                                                 + "was modified by another user after you got the original value. The "
                                                 + "edit operation was canceled and the current values in the database "
                                                 + "have been displayed. If you still want to edit this record, click "
                                                 + "the Save button again. Otherwise click the Back to List hyperlink.");
                        jobToUpdate.RowVersion = databaseValues.RowVersion;
                    }
                }
                catch (RetryLimitExceededException /* dex */)
                {
                    //Log the error (uncomment dex variable name and add a line here to write a log.
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
            ViewBag.EmployeeID = new SelectList(db.Employees, "ID", "FirstName", jobToUpdate.EmployeeID);
            return(View(jobToUpdate));
        }