コード例 #1
0
        public ActionResult Edit(AppUser model)
        {
            List <string> errors = new List <string>();

            try
            {
                var _oldmodel = db.AppUsers.Find(model.Id);
                _oldmodel.UserName   = model.UserName;
                _oldmodel.UserRoleId = model.UserRoleId;
                if (ModelState.IsValid)
                {
                    db.Entry(_oldmodel).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("list"));
                }
                foreach (var item in ModelState.Where(x => x.Value.Errors.Any()))
                {
                    errors.Add(item.Value.Errors.FirstOrDefault().ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
            }
            if (errors.Count > 0)
            {
                TempData["Errors"] = errors;
            }
            ViewBag.UserRoleId = new SelectList(db.UserRoles, "Id", "Name");
            return(View());
        }
コード例 #2
0
        public ActionResult Edit(Costumer model)
        {
            List <string> errors = new List <string>();

            try
            {
                var _Oldmodel = db.Costumers.Find(model.Id);
                _Oldmodel.Name        = model.Name;
                _Oldmodel.GenderId    = model.GenderId;
                _Oldmodel.Address     = model.Address;
                _Oldmodel.Email       = model.Email;
                _Oldmodel.PhoneNumber = model.PhoneNumber;
                //db.Entry(_Oldmodel).State = EntityState.Modified;
                //db.SaveChanges();
                //return RedirectToAction("index");
                if (ModelState.IsValid)
                {
                    db.Entry(_Oldmodel).State = EntityState.Modified;
                    db.SaveChanges();
                    return(RedirectToAction("index"));
                }
                foreach (var item in ModelState.Where(x => x.Value.Errors.Any()))
                {
                    errors.Add(item.Value.Errors.FirstOrDefault().ErrorMessage);
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
            }
            if (errors.Count > 0)
            {
                TempData["Errors"] = errors;
            }
            ViewBag.GenderId = new SelectList(db.Genders, "Id", "Name");
            return(View());
        }