Example #1
0
        public ActionResult EditDirectReports(int?id, EmployeeEditDirectReports newItem)
        {
            // Validate the input
            if (!ModelState.IsValid)
            {
                // Our "version 1" approach is to display the "edit form" again
                return(RedirectToAction("editdirectreports", 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.EmployeeEditDirectReports(newItem);

            if (editedItem == null)
            {
                // There was a problem updating the object
                // Our "version 1" approach is to display the "edit form" again
                return(RedirectToAction("EditDirectReports", new { id = newItem.EmployeeId }));
            }
            else
            {
                // Show the details view, which will have the updated data
                return(RedirectToAction("Details", new { id = newItem.EmployeeId }));
            }
        }
Example #2
0
        public EmployeeWithOrgInfo EmployeeEditDirectReports(EmployeeEditDirectReports newItem)
        {
            // Attention - Update the self-referencing to-many association

            // Attempt to fetch the object

            // When editing an object with a to-many collection,
            // and you wish to edit the collection,
            // MUST fetch its associated collection
            var o = ds.Employees.Include("Employee1").Include("Employee2")
                    .SingleOrDefault(e => e.EmployeeId == newItem.EmployeeId);

            if (o == null)
            {
                // Problem - object was not found, so return
                return(null);
            }
            else
            {
                // Update the object with the incoming values

                // First, clear out the existing collection
                // "Employee1" is the badly-named to-many collection property
                o.Employee1.Clear();

                // Then, go through the incoming items
                // For each one, add to the fetched object's collection
                foreach (var item in newItem.EmployeeIds)
                {
                    var a = ds.Employees.Find(item);
                    o.Employee1.Add(a);
                }
                // Save changes
                ds.SaveChanges();

                return(Mapper.Map <EmployeeWithOrgInfo>(o));
            }
        }
        public EmployeeWithOrgInfo EmployeeEditDirectReports(EmployeeEditDirectReports newItem)
        {
            // Attention - Update the self-referencing to-many association

            // Attempt to fetch the object

            // When editing an object with a to-many collection,
            // and you wish to edit the collection,
            // MUST fetch its associated collection
            var o = ds.Employees.Include("Employee1").Include("Employee2")
                .SingleOrDefault(e => e.EmployeeId == newItem.EmployeeId);

            if (o == null)
            {
                // Problem - object was not found, so return
                return null;
            }
            else
            {
                // Update the object with the incoming values

                // First, clear out the existing collection
                // "Employee1" is the badly-named to-many collection property
                o.Employee1.Clear();

                // Then, go through the incoming items
                // For each one, add to the fetched object's collection
                foreach (var item in newItem.EmployeeIds)
                {
                    var a = ds.Employees.Find(item);
                    o.Employee1.Add(a);
                }
                // Save changes
                ds.SaveChanges();

                return Mapper.Map<EmployeeWithOrgInfo>(o);
            }
        }
        public ActionResult EditDirectReports(int? id, EmployeeEditDirectReports newItem)
        {
            // Validate the input
            if (!ModelState.IsValid)
            {
                // Our "version 1" approach is to display the "edit form" again
                return RedirectToAction("editdirectreports", 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.EmployeeEditDirectReports(newItem);

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