public JsonResult SaveModules(AdminModules entity)
        {
            var result = JsonErrorResult;

            var success = 0;

            if (entity != null)
            {
                var    user     = Membership.GetUser(HttpContext.User.Identity.Name);
                string userName = user.UserName;

                // First delete all entries for user
                var modules = Context.BlogAdminModules.Where(x => x.User.Username == userName);
                foreach (var mod in modules)
                {
                    Context.BlogAdminModules.Remove(mod);
                }

                // Then add the new modules to the user
                if (entity.AdminModulesColumn1 != null)
                {
                    foreach (var module in entity.AdminModulesColumn1)
                    {
                        var thisUser = Context.Users.FirstOrDefault(x => x.Username == userName);

                        // Make sure modules exist
                        checkNullUserModules(thisUser);

                        thisUser.BlogAdminModules.Add(module);
                    }
                }

                if (entity.AdminModulesColumn2 != null)
                {
                    foreach (var module in entity.AdminModulesColumn2)
                    {
                        var thisUser = Context.Users.FirstOrDefault(x => x.Username == userName);

                        // Make sure modules exist
                        checkNullUserModules(thisUser);

                        thisUser.BlogAdminModules.Add(module);
                    }
                }

                success = Context.SaveChanges();
            }
            if (success > 0)
            {
                result.Data = new
                {
                    success = true,
                    message = "Modules updated successfully."
                };
            }
            ;
            return(result);
        }
示例#2
0
        public JsonResult SaveModules(AdminModules moduleLists)
        {
            var user = Membership.GetUser(HttpContext.User.Identity.Name);

            if (moduleLists == null || user == null)
            {
                return(JsonErrorResult);
            }

            var userName = user.UserName;

            Context.BlogAdminModules.RemoveRange(Context.BlogAdminModules.Where(x => x.User.Username == userName));

            var thisUser = Context.Users.FirstOrDefault(x => x.Username == userName);

            CheckNullUserModules(thisUser);

            UpdateModulesUser(moduleLists.AdminModulesColumn1, thisUser);
            UpdateModulesUser(moduleLists.AdminModulesColumn2, thisUser);

            var success = Context.SaveChanges();

            if (success > 0)
            {
                return(new JsonResult
                {
                    Data = new
                    {
                        success = true,
                        message = "Modules updated successfully."
                    }
                });
            }

            return(JsonErrorResult);
        }
示例#3
0
        public JsonResult SaveModules(AdminModules moduleLists)
        {
            var user = Membership.GetUser(HttpContext.User.Identity.Name);

            if (moduleLists == null || user == null)
            {
                return JsonErrorResult;
            }

            var userName = user.UserName;
            Context.BlogAdminModules.RemoveRange(Context.BlogAdminModules.Where(x => x.User.Username == userName));

            var thisUser = Context.Users.FirstOrDefault(x => x.Username == userName);
            CheckNullUserModules(thisUser);

            UpdateModulesUser(moduleLists.AdminModulesColumn1, thisUser);
            UpdateModulesUser(moduleLists.AdminModulesColumn2, thisUser);

            var success = Context.SaveChanges();

            if (success > 0)
            {
                return new JsonResult
                {
                    Data = new
                    {
                        success = true,
                        message = "Modules updated successfully."
                    }
                };
            }

            return JsonErrorResult;
        }
        public JsonResult SaveModules(AdminModules entity)
        {
            var result = new JsonResult();

            if (entity != null)
            {
                var user = Membership.GetUser(HttpContext.User.Identity.Name);
                string userName = user.UserName;

                using (var context = new DataContext())
                {
                    // First delete all entries for user
                    var modules = context.BlogAdminModules.Where(x => x.User.Username == userName);
                    foreach (var mod in modules)
                    {
                        context.BlogAdminModules.Remove(mod);
                    }

                    // Then add the new modules to the user
                    if (entity.AdminModulesColumn1 != null)
                    {
                        foreach (var module in entity.AdminModulesColumn1)
                        {
                            var thisUser = context.Users.FirstOrDefault(x => x.Username == userName);

                            // Make sure modules exist
                            checkNullUserModules(thisUser);

                            thisUser.BlogAdminModules.Add(module);
                        }
                    }

                    if (entity.AdminModulesColumn2 != null)
                    {
                        foreach (var module in entity.AdminModulesColumn2)
                        {
                            var thisUser = context.Users.FirstOrDefault(x => x.Username == userName);

                            // Make sure modules exist
                            checkNullUserModules(thisUser);

                            thisUser.BlogAdminModules.Add(module);
                        }
                    }

                    context.SaveChanges();
                }
            }

            return result;
        }