public ActionResult ThemSua(TaiKhoan taikhoan)
        {
            int id     = taikhoan.Id;
            var errors = Validate(taikhoan);

            if (errors.Count > 0)
            {
                return(Json(new { Errors = errors }, JsonRequestBehavior.AllowGet));
            }
            if (id == 0)
            {
                try
                {
                    db.TaiKhoans.Add(taikhoan);
                    db.SaveChanges();
                    return(Json(new { Id = 0, JsonRequestBehavior.AllowGet }));
                }
                catch (Exception ex)
                {
                    return(null);
                }
            }
            else
            {
                var check = db.TaiKhoans.AsNoTracking().FirstOrDefault(n => n.Id == taikhoan.Id);
                if (check != null)
                {
                    check.Password        = taikhoan.Password;
                    check.VaiTroId        = taikhoan.VaiTroId;
                    db.Entry(check).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                return(Json(new { Id = check.Id, JsonRequestBehavior.AllowGet }));
            }
        }
Exemple #2
0
        public ActionResult ThemSua(Menu menu)
        {
            int id = menu.Id;

            if (id == 0)
            {
                try
                {
                    db.Menus.Add(menu);
                    db.SaveChanges();
                    return(Json(new { Id = 0, JsonRequestBehavior.AllowGet }));
                }
                catch (Exception ex)
                {
                    return(null);
                }
            }
            else
            {
                var check = db.Menus.AsNoTracking().FirstOrDefault(n => n.Id == menu.Id);
                if (check != null)
                {
                    check.MoTa            = menu.MoTa;
                    check.Ten             = menu.Ten;
                    db.Entry(check).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                return(Json(new { Id = check.Id, JsonRequestBehavior.AllowGet }));
            }
        }
        public ActionResult ThemSua(VaiTro vaitro)
        {
            int id     = vaitro.Id;
            var errors = Validate(vaitro);

            if (errors.Count > 0)
            {
                return(Json(new { Errors = errors }, JsonRequestBehavior.AllowGet));
            }
            if (id == 0)
            {
                try
                {
                    db.VaiTroes.Add(vaitro);
                    db.SaveChanges();
                    return(Json(new { Id = 0, JsonRequestBehavior.AllowGet }));
                }
                catch (Exception ex)
                {
                    return(null);
                }
            }
            else
            {
                var check = db.VaiTroes.AsNoTracking().FirstOrDefault(n => n.Id == vaitro.Id);
                if (check != null)
                {
                    check.MenuId = vaitro.MenuId;
                    check.Ten    = vaitro.Ten;
                    if (check.PhanQuyens.Count > 0)
                    {
                        foreach (var pd in check.PhanQuyens.ToList())
                        {
                            db.Entry(pd).State = EntityState.Deleted;
                        }
                    }
                    foreach (var item in vaitro.PhanQuyens)
                    {
                        db.PhanQuyens.Add(item);
                    }
                    db.Entry(check).State = EntityState.Modified;
                    db.SaveChanges();
                }
                return(Json(new { Id = check.Id, JsonRequestBehavior.AllowGet }));
            }
        }
Exemple #4
0
        public ActionResult ThemSua(UserViewModel user)
        {
            int id     = user.Id;
            var errors = Validate(user);

            if (errors.Count > 0)
            {
                return(Json(new { Errors = errors }, JsonRequestBehavior.AllowGet));
            }
            if (user.AvatarFile != null)
            {
                byte[] fileInBytes = new byte[user.AvatarFile.ContentLength];
                using (BinaryReader theReader = new BinaryReader(user.AvatarFile.InputStream))
                {
                    fileInBytes = theReader.ReadBytes(user.AvatarFile.ContentLength);
                }
                user.Avatar = "data:image/jpeg;base64," + Convert.ToBase64String(fileInBytes);
            }
            if (id == 0)
            {
                try
                {
                    var data = _iMapper.Map <UserViewModel, User>(user);
                    db.Users.Add(data);
                    db.SaveChanges();
                    return(Json(new { Id = 0, JsonRequestBehavior.AllowGet }));
                }
                catch (Exception ex)
                {
                    return(null);
                }
            }
            else
            {
                var check = db.Users.AsNoTracking().FirstOrDefault(n => n.Id == user.Id);
                if (check != null)
                {
                    check = _iMapper.Map <UserViewModel, User>(user);
                    db.Entry(check).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
                return(Json(new { Id = check.Id, JsonRequestBehavior.AllowGet }));
            }
        }