AssocSelf.Controllers.Manager.EmployeeEditSupervisor C# (CSharp) Method

EmployeeEditSupervisor() public method

public EmployeeEditSupervisor ( EmployeeEditSupervisor newItem ) : EmployeeWithOrgInfo
newItem EmployeeEditSupervisor
return EmployeeWithOrgInfo
        public EmployeeWithOrgInfo EmployeeEditSupervisor(EmployeeEditSupervisor newItem)
        {
            // Attention - Update the self-referencing to-one association

            // Attempt to fetch the object
            var o = ds.Employees.Include("Employee1").Include("Employee2")
                .SingleOrDefault(e => e.EmployeeId == newItem.EmployeeId);

            // Attempt to fetch the associated object
            Employee a = null;
            if (newItem.ReportsToId > 0)
            {
                a = ds.Employees.Include("Employee1").Include("Employee2")
                    .SingleOrDefault(e => e.EmployeeId == newItem.ReportsToId);
            }

            // Must do two tests here before continuing
            if (o == null | a == null)
            {
                // Problem - one of the items was not found, so return
                return null;
            }
            else
            {
                // Update the object with the incoming values
                // This will handle the standard properties
                // We could also specifically target the new/updated properties
                ds.Entry(o).CurrentValues.SetValues(newItem);

                // Configure the new supervisor
                // MUST set both properties - the int and the Employee
                o.Employee2 = a;
                o.ReportsTo = a.EmployeeId;

                ds.SaveChanges();

                // Prepare and return the object
                return Mapper.Map<EmployeeWithOrgInfo>(o);
            }
        }

Usage Example

Ejemplo n.º 1
0
        public ActionResult EditSupervisor(int?id, EmployeeEditSupervisor newItem)
        {
            // Validate the input
            if (!ModelState.IsValid)
            {
                // Our "version 1" approach is to display the "edit form" again
                return(RedirectToAction("editsupervisor", new { id = newItem.EmployeeId }));
            }

            if (id.GetValueOrDefault() != newItem.EmployeeId)
            {
                // This appears to be data tampering, so redirect the user away
                return(RedirectToAction("index"));
            }

            // Attempt to do the update
            var editedItem = m.EmployeeEditSupervisor(newItem);

            if (editedItem == null)
            {
                // There was a problem updating the object
                // Our "version 1" approach is to display the "edit form" again
                return(RedirectToAction("editsupervisor", new { id = newItem.EmployeeId }));
            }
            else
            {
                // Show the details view, which will have the updated data
                return(RedirectToAction("details", new { id = newItem.EmployeeId }));
            }
        }