Exemple #1
0
        public ActionResult Edit([Bind(Include = "Id,AccountId,Balance,RowVersion")] Bank bank)
        {
            if (ModelState.IsValid)
            {
                var entity = db.Banks.Find(bank.Id);
                if (entity == null)
                {
                    ModelState.AddModelError(string.Empty, "Unable to save changes. The bank was deleted by another user.");
                    return(View(bank));
                }
                else
                {
                    try
                    {
                        entity.Balance = bank.Balance;
                        db.Entry(entity).OriginalValues["RowVersion"] = bank.RowVersion;
                        db.Entry(entity).State = EntityState.Modified;
                        db.SaveChanges();

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

                            if (databaseValues.Balance != clientValues.Balance)
                            {
                                ModelState.AddModelError("Balance", "Current value: "
                                                         + databaseValues.Balance);
                            }
                            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.");
                            bank.RowVersion = databaseValues.RowVersion;
                        }
                    }
                }
            }
            ViewBag.AccountId = new SelectList(db.Accounts, "Id", "Name", bank.AccountId);
            return(View(bank));
        }
Exemple #2
0
 public ActionResult Edit([Bind(Include = "Id,Name")] Role role)
 {
     if (ModelState.IsValid)
     {
         db.Entry(role).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(role));
 }
 public ActionResult Edit([Bind(Include = "Id,UserId,RoleId")] UserRole userRole)
 {
     if (ModelState.IsValid)
     {
         db.Entry(userRole).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.RoleId = new SelectList(db.Roles, "Id", "Name", userRole.RoleId);
     ViewBag.UserId = new SelectList(db.Users, "Id", "Name", userRole.UserId);
     return(View(userRole));
 }
 public ActionResult Edit([Bind(Include = "Id,Name,PhoneNumber,Address")] Account account)
 {
     if (ModelState.IsValid)
     {
         try
         {
             db.Entry(account).State = EntityState.Modified;
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         catch (Exception ex)
         {
             ModelState.AddModelError("", ex.Message);
         }
     }
     return(View(account));
 }