Пример #1
0
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {   //old code---paei sthn DB KAI Kanei kanei query(2 fores sthn DB)
                //Student student = db.Students.Find(id);
                //db.Students.Remove(student); ---flaggarei to state tou se DELETED---
                //kai tha ginei delete sthn DB sthn apo katw grammh sthn grammh saveChanges();


                //new code(extra pou egrapsa)
                //to idio me to apo panw kalutera grammeno
                //--petyxainw ton idio skopo--
                //kaluteros tropos
                //paw mia fora sthn DB
                Student studentToDelete = new Student()
                {
                    ID = id
                };
                db.Entry(studentToDelete).State = EntityState.Deleted;

                db.SaveChanges();
            }
            catch (DataException)
            {
                return(RedirectToAction("Delete", new { id = id, saveChangesError = true })); //gia na tairiaxei me thn getdelete //an den paei kala na xanagyrisei sto DELETE(get)
            }

            return(RedirectToAction("Index"));   //An ola pane kala Tha epistrepsei sto index me -1 student
        }
Пример #2
0
        public async Task <IActionResult> PutEmployee(int id, Employee employee)
        {
            if (id != employee.EmployeeId)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
Пример #3
0
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                // old Code
                //Student student = db.Students.Find(id);
                //db.Students.Remove(student);
                // new Code
                Student studentToDelete = new Student()
                {
                    ID = id
                };
                db.Entry(studentToDelete).State = EntityState.Deleted;

                db.SaveChanges();
            }
            catch (DataException)
            {
                return(RedirectToAction("Delete", new { id = id, saveChangesError = true }));
            }

            return(RedirectToAction("Index"));
        }
Пример #4
0
        public async Task <ActionResult> Edit(int?id, byte[] rowVersion)
        {
            // check id null
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // These are the fields to send To TryToUpdateModel
            string[] fieldsToBind = new string[] { "Name", "Budget", "StartDate", "InstructorID", "RowVersion" };

            // Go find the department to update
            var departmentToUpdate = await db.Departments.FindAsync(id);

            // if department has already been deleted
            if (departmentToUpdate == null)
            {
                // Stay here, show a message, and values that you wanted to update
                Department deletedDepartment = new Department();
                TryUpdateModel(deletedDepartment, fieldsToBind);
                ModelState.AddModelError(string.Empty,
                                         "Unable to save changes. The department was deleted.");
                ViewBag.InstructorID = new SelectList(db.Instructors, "ID", "FullName", deletedDepartment.InstructorID);
                return(View(deletedDepartment));
            }

            // Department exists
            if (TryUpdateModel(departmentToUpdate, fieldsToBind))
            {
                // Normal Update...No one has changed my database record
                try
                {
                    db.Entry(departmentToUpdate).OriginalValues["RowVersion"] = rowVersion;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                catch (DbUpdateConcurrencyException ex)
                {
                    // Find Client Values
                    var entry        = ex.Entries.Single();
                    var clientValues = (Department)entry.Entity;
                    // Find Database Values
                    var databaseEntry = entry.GetDatabaseValues();
                    // Do i need to check for null in database?
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty,
                                                 "Unable to save changes. Department was deleted.");
                    }
                    // Compare values and show what we have on database if we have difference
                    else
                    {
                        var databaseValues = (Department)databaseEntry.ToObject();

                        if (databaseValues.Name != clientValues.Name)
                        {
                            ModelState.AddModelError("Name", "Current Value: " + databaseValues.Name);
                        }
                        if (databaseValues.Budget != clientValues.Budget)
                        {
                            ModelState.AddModelError("Budget", "Current Value: " + databaseValues.Budget);
                        }
                        if (databaseValues.StartDate != clientValues.StartDate)
                        {
                            ModelState.AddModelError("StartDate", "Current Value: " + databaseValues.StartDate);
                        }
                        if (databaseValues.InstructorID != clientValues.InstructorID)
                        {
                            ModelState.AddModelError("InstructorID", "Current Value: "
                                                     + db.Instructors.Find(databaseValues.InstructorID).FullName);
                        }
                        // set rowversion to the right state
                        departmentToUpdate.RowVersion = databaseValues.RowVersion;
                    }
                }
            }

            ViewBag.InstructorID = new SelectList(db.Instructors, "ID", "FullName", departmentToUpdate.InstructorID);
            return(View(departmentToUpdate));
        }