public ActionResult CreateUser(FormCollection form)
        {
            var userDetailsModel = new UserDetailsModel()
            {
                UserId = form["UserId"],
                BU = form["BU"],
                GPN = form["GPN"],
                LocationId = form["LocationId"],
                Mail = form["Mail"],
                SMU = form["SMU"],
                UserName = form["UserName"]
            };

            var permissionLocation = new List<PermissionLocationModel>();

            foreach (var key in form.Keys)
            {
                if (key.ToString().StartsWith("permissionLocation"))
                {
                    var values = form[key.ToString()].Split(';');

                    var locationId = values[0];
                    var permissionId = int.Parse(values[1]);


                    permissionLocation.Add(new PermissionLocationModel()
                    {
                        LocationId = locationId,
                        PermissionId = permissionId
                    });
                }
            }

            userDetailsModel.Permissions = permissionLocation;

            using (var context = new Data.QUERYEntities())
            {
                var user = new User()
                {
                    BU = userDetailsModel.BU,
                    GPN = userDetailsModel.GPN,
                    LocationID = userDetailsModel.LocationId,
                    Mail = userDetailsModel.Mail,
                    Name = userDetailsModel.UserName,
                    SMU = userDetailsModel.SMU,
                    PermissionType = "GPN",
                    Date = DateTime.Now,
                    UserID = userDetailsModel.UserId
                };

                foreach (var up in userDetailsModel.Permissions)
                {
                    user.UserPermissions.Add(new UserPermission()
                    {
                        PermissionID = up.PermissionId,
                        LocalID = up.LocationId
                    });
                }

                context.Users.Add(user);
                try
                {
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    var q = ex;
                    throw;
                }
                
            }

            return RedirectToAction("EditUser", new { userId = userDetailsModel.UserId});
        }
        public ActionResult EditUser(FormCollection form)
        {
            var userDetailsModel = new UserDetailsModel()
            {
                BU = form["UserDetails.BU"],
                GPN = form["UserDetails.GPN"],
                LocationId = form["UserDetails.LocationId"],
                Mail = form["UserDetails.Mail"],
                SMU = form["UserDetails.SMU"],
                UserId = form["UserDetails.UserId"],
                UserName = form["UserDetails.UserName"]
            };

            var permissionLocation = new List<PermissionLocationModel>();

            foreach (var key in form.Keys)
            {
                if (key.ToString().StartsWith("permissionLocation"))
                {
                    var values = form[key.ToString()].Split(';');

                    var locationId = values[0];
                    var permissionId = int.Parse(values[1]);
                    

                    permissionLocation.Add(new PermissionLocationModel()
                    {
                        LocationId = locationId,
                        PermissionId = permissionId
                    });   
                }
            }

            userDetailsModel.Permissions = permissionLocation;

            using (var context = new Data.QUERYEntities())
            {
                var user = context.Users.Single(u => u.UserID == userDetailsModel.UserId);

                user.BU = userDetailsModel.BU;
                user.GPN = userDetailsModel.GPN;
                user.LocationID = userDetailsModel.LocationId;
                user.Mail = userDetailsModel.Mail;
                user.Name = userDetailsModel.UserName;
                user.SMU = userDetailsModel.SMU;

                foreach (var up in context.UserPermissions.Where(up => up.UserID == userDetailsModel.UserId))
                {
                    context.UserPermissions.Remove(up);
                }

                foreach (var up in userDetailsModel.Permissions)
                {
                    context.UserPermissions.Add(new UserPermission()
                    {
                        UserID = userDetailsModel.UserId,
                        PermissionID = up.PermissionId,
                        LocalID = up.LocationId
                    });
                }

                context.SaveChanges();
            }


            return EditUser(userDetailsModel.UserId);
        }