Пример #1
0
        // Enable initialization with an instance of ApplicationUser:
        public SelectUserRolesViewModel(ApplicationUser user)
            : this()
        {
            this.UserName = user.UserName;
            this.FirstName = user.FirstName;
            this.LastName = user.LastName;

            var Db = new commenergyContext();

            // Add all available roles to the list of EditorViewModels:
            var allRoles = Db.Roles;
             foreach(var role in allRoles)
            {

                // An EditorViewModel will be used by Editor Template:
                var rvm = new SelectRoleEditorViewModel(role);
                this.Roles.Add(rvm);
            }

            // Set the Selected property to true for those roles for
            // which the current user is a member:
            foreach (var userRole in Db.Roles)
            {
                var checkUserRole =
                    Roles.Find(r => r.RoleId == userRole.Id);
                checkUserRole.Selected = true;
            }
        }
Пример #2
0
 public JsonResult UserRoles(string UserName)
 {
     var Db = new commenergyContext();
     var user = Db.Users.First(u => u.UserName == UserName);
     var model = new SelectUserRolesViewModel(user);
     return Json(model, JsonRequestBehavior.AllowGet);
 }
Пример #3
0
 public JsonResult UserRoleEdit(SelectUserRolesViewModel model)
 {
     //if (ModelState.IsValid)
     {
         var idManager = new IdentityManager();
         var Db = new commenergyContext();
         var user = Db.Users.First(u => u.UserName == model.UserName);
         idManager.ClearUserRoles(user.Id);
         foreach (var role in model.Roles)
         {
             if (role.Selected)
             {
                 idManager.AddUserToRole(user.Id, role.RoleName);
                 Db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                 Db.SaveChangesAsync();
             }
             return Json(model, JsonRequestBehavior.AllowGet);
         }
     }
     return Json(model, JsonRequestBehavior.AllowGet);
 }
Пример #4
0
 public JsonResult DeleteConfirmed(string UserName)
 {
     var Db = new commenergyContext();
     var user = Db.Users.FirstOrDefault(u => u.UserName == UserName);
     Db.Users.Remove(user);
     Db.SaveChanges();
     return Json("Index");
 }
Пример #5
0
        public async Task<ActionResult> Edit(EditUserViewModel model)
        {
            if (ModelState.IsValid)
            {
                var Db = new commenergyContext();
                var user = Db.Users.First(u => u.UserName == model.UserName);
                user.FirstName = model.FirstName;
                user.LastName = model.LastName;
                user.Email = model.Email;
                Db.Entry(user).State = System.Data.Entity.EntityState.Modified;
                await Db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Пример #6
0
 public ActionResult Edit(string id, ManageMessageId? Message = null)
 {
     var Db = new commenergyContext();
     var user = Db.Users.First(u => u.UserName == id);
     var model = new EditUserViewModel(user);
     ViewBag.MessageId = Message;
     return View(model); 
 }
Пример #7
0
        public JsonResult UserIndex()
        {


            var Db = new commenergyContext();
            var Users = Db.Users;
            var model = new List<EditUserViewModel>();
            foreach (var user in Users)
            {
                var u = new EditUserViewModel(user);
                model.Add(u);
            }
            return Json(model, JsonRequestBehavior.AllowGet);


        }
Пример #8
0
        public ViewResult UserAdmin()
        {

            var Db = new commenergyContext();
            var Users = Db.Users;
            var model = new List<EditUserViewModel>();
            foreach (var user in Users)
            {
                var u = new EditUserViewModel(user);
                model.Add(u);
            }
            return View(model);
        }