コード例 #1
0
        public ActionResult DeleteMenu(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                ExtendedMenu menu = new ExtendedMenu();

                try
                {
                    // Checkbox is the attribute name.
                    // Returns values of the boxes that have been checked as csv.
                    string[] checkboxes = form["checkbox"].Split(',');
                    foreach (var menuID in checkboxes)
                    {
                        int  iMenuID    = Int32.Parse(menuID);
                        Menu deleteMenu = db.Menus.SingleOrDefault(x => x.menuID == iMenuID);
                        db.Menus.Remove(deleteMenu);
                        db.SaveChanges();
                    }
                    menu.menuList       = db.Menus.ToList();
                    menu.menuTableList  = new List <Menu>();
                    TempData["Message"] = "One or more user has been successfully deleted!";
                    return(View("MenuMaintenance", menu));
                }
                catch (Exception e) // Catch exception when no item from table was selected.
                {
                    menu.menuList       = db.Menus.ToList();
                    menu.menuTableList  = new List <Menu>();
                    TempData["Message"] = "Error: No item was selected from table.";
                    return(View("MenuMaintenance", menu));
                }
            }
        }
コード例 #2
0
        public ActionResult ChangePassword(ExtendedAccount acc)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                using (MD5 md5Hash = MD5.Create())
                {
                    string hash        = Encryption.GetMd5Hash(md5Hash, acc.password);
                    var    userDetails = db.Accounts.SingleOrDefault(x => x.username == acc.username && x.password == hash);

                    if (userDetails == null)
                    {
                        HttpCookie cookie = Request.Cookies["Profile"];
                        if (cookie != null)
                        {
                            ViewBag.username = cookie["username"].ToString();
                        }
                        TempData["Message"] = "Wrong Username or Password.";
                        return(View("ChangePassword"));
                    }
                    else
                    {
                        userDetails.password = hash;
                        db.SaveChanges();

                        TempData["Message"] = "Password changed successfully!";
                        return(View("ChangePassword"));
                    }
                }
            }
        }
コード例 #3
0
        public ActionResult QueryUser(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                ExtendedUser users = new ExtendedUser();
                users.userList      = db.Users.ToList();
                users.roleList      = db.Roles.ToList();
                users.userTableList = new List <User>();

                var selectUser = form["selectUser"];
                var selectRole = form["selectRole"];

                var queryUser = db.Users.AsQueryable();
                if (selectUser != "")
                {
                    int iSelectUser = int.Parse(selectUser);
                    queryUser = queryUser.Where(x => x.userID == iSelectUser);
                }
                if (selectRole != "")
                {
                    int iSelectRole = int.Parse(selectRole);
                    queryUser = queryUser.Where(x => x.roleID == iSelectRole);
                }

                users.userTableList = queryUser.ToList();
                return(View("UserMaintenance", users));
            }
        }
コード例 #4
0
        public ActionResult ModifyUserDatabase(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                int sessionUserID = (int)Session["userID"];
                var sessionUser   = db.Accounts.SingleOrDefault(x => x.userID == sessionUserID);

                var userID       = form["selectUser"];
                var roleID       = form["selectRole"];
                var selectStatus = form["selectStatus"];
                int iUserID      = int.Parse(userID);
                int iRoleID      = int.Parse(roleID);

                User user = db.Users.SingleOrDefault(x => x.userID == iUserID);
                user.roleID = iRoleID;
                user.status = false;
                if (selectStatus == "1")
                {
                    user.status = true;
                }
                DateTime curretDateTime = DateTime.Now;
                user.modifyDate = curretDateTime;
                user.modifyUser = sessionUser.username;
                db.SaveChanges();

                ExtendedUser users = new ExtendedUser();
                users.userList      = db.Users.ToList();
                users.roleList      = db.Roles.Where(x => x.status == true).ToList();
                users.userTableList = new List <User>();
                TempData["Message"] = "Role has been successfully modified!";
                return(View("UserMaintenance", users));
            }
        }
コード例 #5
0
        public ActionResult DeleteUser(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                ExtendedUser users = new ExtendedUser();
                try
                {
                    // Checkbox is the attribute name.
                    // Returns values of the boxes that have been checked as csv.
                    string[] checkboxes = form["checkbox"].Split(',');

                    foreach (var user in checkboxes)
                    {
                        int  iUserID   = Int32.Parse(user);
                        User deletUser = db.Users.SingleOrDefault(x => x.userID == iUserID);
                        db.Users.Remove(deletUser);
                        db.SaveChanges();
                    }
                    users.userList      = db.Users.ToList();
                    users.roleList      = db.Roles.Where(x => x.status == true).ToList();
                    users.userTableList = new List <User>();
                    TempData["Message"] = "One or more user has been successfully deleted!";
                    return(View("UserMaintenance", users));
                }
                catch (Exception e) // Catch exception when no item from table was selected.
                {
                    users.userList      = db.Users.ToList();
                    users.roleList      = db.Roles.Where(x => x.status == true).ToList();
                    users.userTableList = new List <User>();
                    TempData["Message"] = "Error: No item was selected from table.";
                    return(View("UserMaintenance", users));
                }
            }
        }
コード例 #6
0
        public ActionResult Save(string encodedHtml)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                System.Diagnostics.Debug.WriteLine("--> " + encodedHtml);
                string myDecodedString = Uri.UnescapeDataString(encodedHtml);
                System.Diagnostics.Debug.WriteLine("--> " + myDecodedString);

                var  userID = (int)Session["userID"];
                Home editor = new Home();
                if (db.Homes.SingleOrDefault(x => x.userID == userID) != null)
                {
                    editor       = db.Homes.SingleOrDefault(x => x.userID == userID);
                    editor.home1 = myDecodedString;
                }
                else
                {
                    editor.userID = userID;
                    editor.home1  = myDecodedString;
                    db.Homes.Add(editor);
                }
                db.SaveChanges();

                return(Json(new { newUrl = Url.Action("Index", "Home") }, JsonRequestBehavior.AllowGet));
            }
        }
コード例 #7
0
 public ActionResult ModifyRole(FormCollection form)
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         ExtendedRole viewModel = new ExtendedRole();
         viewModel.rolesList      = db.Roles.ToList();
         viewModel.rolesTableList = db.Roles.ToList();
         try
         {
             // Checkbox is the attribute name.
             // Returns values of the boxes that have been checked as csv.
             string[] checkboxes = form["checkbox"].Split(',');
             if (checkboxes.Length != 1) // More than 1 checkbox selected.
             {
                 TempData["Message"] = "Error: Can only select one to modify from table";
                 return(View("RoleMaintenance", viewModel));
             }
             else
             {
                 int  iSelectRole = int.Parse(checkboxes[0]);
                 Role role        = db.Roles.SingleOrDefault(x => x.roleID == iSelectRole);
                 return(View("ModifyRole", role));
             }
         }
         catch (Exception e) // Catch exception when no item from table was selected.
         {
             TempData["Message"] = "Error: No item was selected from table.";
             return(View("RoleMaintenance", viewModel));
         }
     }
 }
コード例 #8
0
ファイル: HomeController.cs プロジェクト: Observers/Homework2
 public ActionResult CKEditor()
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         var  userID = (int)Session["userID"];
         Home editor = db.Homes.SingleOrDefault(x => x.userID == userID);
         return(View(editor));
     }
 }
コード例 #9
0
 public ActionResult AddUser()
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         ExtendedRole roles = new ExtendedRole();
         roles.rolesList = db.Roles.Where(x => x.status == true).ToList();
         return(View(roles));
     }
 }
コード例 #10
0
 public ActionResult AddMenu()
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         ExtendedMenu menu = new ExtendedMenu();
         menu.menuList = db.Menus.SqlQuery("SELECT * FROM Menu m1 INNER JOIN Menu m2 ON m1.menuID = m2.menuID WHERE m1.menuNo LIKE '00%' AND LEN(m2.MenuNo) = 3").ToList();
         return(View(menu));
     }
 }
コード例 #11
0
 // Menu Maintenance landing page.
 public ActionResult MenuMaintenance(ExtendedMenu menus)
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         menus.menuList      = db.Menus.ToList();
         menus.menuTableList = new List <Menu>();
         return(View("../MenuMaintenance/MenuMaintenance", menus));
     }
 }
コード例 #12
0
 // User Maintenance landing page.
 public ActionResult UserMaintenance(ExtendedUser users)
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         users.userList      = db.Users.ToList();
         users.roleList      = db.Roles.ToList();
         users.userTableList = new List <User>();
         return(View("../UserMaintenance/UserMaintenance", users));
     }
 }
コード例 #13
0
 // User Maintenance landing page.
 public ActionResult UserMaintenance(ExtendedUser users)
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         users.userList      = db.Users.ToList();
         users.roleList      = db.Roles.Where(x => x.status == true).ToList();
         users.userTableList = new List <User>();
         return(View(users));
     }
 }
コード例 #14
0
 // Role Maintenance landing page.
 public ActionResult RoleMaintenance(ExtendedRole roles)
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         roles.rolesList = db.Roles.ToList();
         List <Role> list = new List <Role>();
         roles.rolesTableList = list;
         return(View(roles));
     }
 }
コード例 #15
0
 // Menu Maintenance landing page.
 public ActionResult MenuMaintenance(ExtendedMenu menus)
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         // Query database first to get data for dropdown list.
         menus.menuList      = db.Menus.ToList();
         menus.menuTableList = new List <Menu>();
         return(View(menus));
     }
 }
コード例 #16
0
        //This function identifies the next menu number for the new added menu.
        //Note: This only works when main menu does not exceed 9 'Menu' pages.
        //      Each sub - menu does not exceed 9 'Program' pages
        public string MenuNo(string linkType, string subMenu)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                string menuNo = "00";

                int iEndNo = 0;
                if (linkType == "1") // Main Menu
                {
                    // SQL selecting all menu where the menuNo is 3 digits from 000 - 009 order number by
                    // biggest to smallest.
                    string temp  = "SELECT * FROM Menu WHERE menuNo LIKE '" + menuNo + "[0-9]' ORDER BY menuNo DESC";
                    var    endNo = db.Menus.SqlQuery(temp).ToList();
                    if (endNo != null)
                    {
                        // Gets the first menu from the list.
                        Menu menu1 = endNo.First();
                        // Gets the menuNo.
                        iEndNo = int.Parse(menu1.menuNo);
                        // Increase the endNo.
                        iEndNo++;
                        // Creates the new menuNo string.
                        menuNo += iEndNo;
                    }
                    else // If it's the first main menu to be added.
                    {
                        iEndNo++;
                        menuNo += iEndNo;
                    }
                }
                else // Sub-menu
                {
                    // SQL selecting all menu where the menuNo is 3 digits from 0000 - 0009 order number by
                    // biggest to smallest.
                    string temp  = "SELECT * FROM Menu WHERE menuNo LIKE '" + subMenu + "[0-9]' ORDER BY menuNo DESC";
                    var    endNo = db.Menus.SqlQuery(temp).ToList();
                    if (endNo != null)
                    {
                        Menu menu1 = endNo.First();
                        iEndNo = int.Parse(menu1.menuNo);
                        iEndNo++;
                        menuNo += iEndNo;
                    }
                    else // First sub-menu added to main-menu.
                    {
                        iEndNo++;
                        menuNo += iEndNo;
                    }
                }
                return(menuNo);
            }
        }
コード例 #17
0
        public ActionResult MenuRoleDatabase(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                var          roleID    = int.Parse(form["selectRole"]);
                ExtendedRole viewModel = new ExtendedRole();
                try
                {
                    // Checkbox is the attribute name.
                    // Returns values of the boxes that have been checked as csv.
                    string[] checkboxes = form["checkbox"].Split(',');

                    var menus = db.Menus.ToList();
                    var role  = db.Roles.SingleOrDefault(x => x.roleID == roleID);
                    foreach (var menu in menus)
                    {
                        // Add to menu to role.
                        if (checkboxes.Contains(menu.menuID.ToString()))
                        {
                            if (!menu.Roles.Contains(role))
                            {
                                db.Menus.SingleOrDefault(x => x.menuID == menu.menuID).Roles.Add(role);
                                db.SaveChanges();
                            }
                        }
                        // Remove menu from role.
                        if (!checkboxes.Contains(menu.menuID.ToString()))
                        {
                            if (menu.Roles.Contains(role))
                            {
                                db.Roles.SingleOrDefault(x => x.roleID == role.roleID).Menus.Remove(menu);
                                db.SaveChanges();
                            }
                        }
                    }

                    viewModel.rolesList = db.Roles.ToList();
                    List <Role> list = new List <Role>();
                    viewModel.rolesTableList = list;
                    TempData["Message"]      = " Role - Menu updated.";
                    return(View("RoleMaintenance", viewModel));
                }
                catch (Exception e) // Catch exception when no item from table was selected.
                {
                    viewModel.role      = db.Roles.SingleOrDefault(x => x.roleID == roleID);
                    viewModel.menu      = db.Menus.ToList();
                    viewModel.menuRole  = viewModel.role.Menus.ToList();
                    TempData["Message"] = "Error: No item was selected from table.";
                    return(View("MenuRole", viewModel));
                }
            }
        }
コード例 #18
0
        public ActionResult AddRole(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                // Calling database function down below.
                Database(form, true);

                ExtendedRole viewModel = new ExtendedRole();
                viewModel.rolesList = db.Roles.ToList();
                List <Role> list = new List <Role>();
                viewModel.rolesTableList = list;
                TempData["Message"]      = "Role has been successfully added!";
                return(View("RoleMaintenance", viewModel));
            }
        }
コード例 #19
0
        public ActionResult Login(ExtendedAccount acc, FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                using (MD5 md5Hash = MD5.Create())
                {
                    // Encode text password into MD5 hash.
                    string hash = Encryption.GetMd5Hash(md5Hash, acc.password);

                    HttpCookie cookie = new HttpCookie("Profile");
                    string     f      = form["RememberMe"];
                    if (f != null)
                    {
                        cookie["username"] = acc.username;
                        // Encrypt MD5 password into ASCII and save to cookie.
                        byte[] b = ASCIIEncoding.ASCII.GetBytes(acc.password);
                        string EncryptedPassword = Convert.ToBase64String(b);
                        cookie["password"] = EncryptedPassword;
                        // Set expire timmer to cookie.
                        cookie.Expires = DateTime.Now.AddDays(2);
                        HttpContext.Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        // Remove any cookie related to this website.
                        cookie.Expires = DateTime.Now.AddDays(-1);
                        HttpContext.Response.Cookies.Add(cookie);
                    }

                    var userDetails = db.Accounts.SingleOrDefault(x => x.username == acc.username && x.password == hash && x.User.status == true);
                    if (userDetails == null)
                    {
                        TempData["Message"] = "Access denied: Username or password is incorrect or you don't have an account.";
                        return(View("Index"));
                    }
                    else
                    {
                        // Store neccessary information in session that will be used in all websites without having to
                        // continuously call the same information from database.
                        Session["userID"]   = userDetails.userID;
                        Session["username"] = userDetails.username;
                        Session["mainMenu"] = userDetails.User.Role.Menus.Where(x => x.level == 0 || x.level == 1).OrderBy(x => x.menuNo).ToList();
                        Session["subMenu"]  = userDetails.User.Role.Menus.Where(x => x.level == 2).ToList();
                        return(RedirectToAction("Index", "Home"));
                    }
                }
            }
        }
コード例 #20
0
        public ActionResult AddUser(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                int      sessionUserID  = (int)Session["userID"];
                var      sessionUser    = db.Accounts.SingleOrDefault(x => x.userID == sessionUserID);
                var      username       = form["username"];
                var      roleId         = form["selectRole"];
                int      iRoleID        = int.Parse(roleId);
                var      selectStatus   = form["selectStatus"];
                DateTime curretDateTime = DateTime.Now;

                Account newAccount = new Account();
                newAccount.username = username;
                string newPassword = username + "12345";
                using (MD5 md5Hash = MD5.Create())
                {
                    string hash = Encryption.GetMd5Hash(md5Hash, newPassword);
                    newAccount.password = hash;
                    db.Accounts.Add(newAccount);
                }

                User addUser = new User();
                addUser.username = username;
                addUser.roleID   = iRoleID;
                addUser.status   = false;
                if (selectStatus == "1")
                {
                    addUser.status = true;
                }
                addUser.createDate = curretDateTime;
                addUser.createUser = sessionUser.username;
                addUser.modifyDate = curretDateTime;
                addUser.modifyUser = sessionUser.username;
                db.Users.Add(addUser);
                db.SaveChanges();

                ExtendedUser users = new ExtendedUser();
                users.userList      = db.Users.ToList();
                users.roleList      = db.Roles.Where(x => x.status == true).ToList();
                users.userTableList = new List <User>();
                TempData["Message"] = "A new user has been successfully added!";
                return(View("UserMaintenance", users));
            }
        }
コード例 #21
0
        // Database function to add and modify records accordingly.
        public void Database(FormCollection form, bool action)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                int sessionUserID = (int)Session["userID"];
                var sessionUser   = db.Accounts.SingleOrDefault(x => x.userID == sessionUserID);

                Role role = new Role();

                if (action) // Add role.
                {
                    role.roleName = form["roleName"];
                }
                if (!action) // Modify role.
                {
                    var selectRole  = form["selectRole"];
                    int iSelectRole = int.Parse(selectRole);
                    role = db.Roles.SingleOrDefault(x => x.roleID == iSelectRole);
                }

                role.description = form["description"];
                var selectStatus = form["selectStatus"];
                role.status = false;
                if (selectStatus == "1")
                {
                    role.status = true;
                }

                DateTime curretDateTime = DateTime.Now;
                if (action) // Add role
                {
                    role.createDate = curretDateTime;
                    role.createUser = sessionUser.username;
                }
                role.modifyDate = curretDateTime;
                role.modifyUser = sessionUser.username;

                if (action) // Add role
                {
                    db.Roles.Add(role);
                }

                db.SaveChanges();
            }
        }
コード例 #22
0
 public ActionResult QueryRole(FormCollection form)
 {
     using (Trainee15Entities db = new Trainee15Entities())
     {
         var selectRole = form["selectRole"];
         var viewModel  = new ExtendedRole();
         if (selectRole != "")
         {
             int iSelectRole = int.Parse(selectRole);
             viewModel.rolesList      = db.Roles.ToList();
             viewModel.rolesTableList = db.Roles.Where(x => x.roleID == iSelectRole).ToList();
         }
         else
         {
             viewModel.rolesList      = db.Roles.ToList();
             viewModel.rolesTableList = db.Roles.ToList();
         }
         return(View("RoleMaintenance", viewModel));
     }
 }
コード例 #23
0
        public ActionResult DeleteRole(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                ExtendedRole viewModel = new ExtendedRole();

                try
                {
                    // Checkbox is the attribute name.
                    // Returns values of the boxes that have been checked as csv.
                    string[] checkboxes = form["checkbox"].Split(',');
                    foreach (var role in checkboxes)
                    {
                        int iRoleID = Int32.Parse(role);
                        // Delete role-menu relationship first before deleting roles.
                        List <Menu> dropMenus = db.Roles.SingleOrDefault(r => r.roleID == iRoleID).Menus.ToList();
                        foreach (var drop in dropMenus)
                        {
                            db.Roles.SingleOrDefault(r => r.roleID == iRoleID).Menus.Remove(drop);
                        }
                        Role deleteRole = db.Roles.SingleOrDefault(r => r.roleID == iRoleID);
                        db.Roles.Remove(deleteRole);
                        db.SaveChanges();
                    }
                    viewModel.rolesList = db.Roles.ToList();
                    List <Role> list = new List <Role>();
                    viewModel.rolesTableList = list;
                    TempData["Message"]      = "One or more role has been successfully deleted!";
                    return(View("RoleMaintenance", viewModel));
                }
                catch (Exception e) // Catch exception when no item from table was selected.
                {
                    viewModel.rolesList = db.Roles.ToList();
                    List <Role> list = new List <Role>();
                    viewModel.rolesTableList = list;
                    TempData["Message"]      = "Error: No item was selected from table.";
                    return(View("RoleMaintenance", viewModel));
                }
            }
        }
コード例 #24
0
        public ActionResult ModifyMenu(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                ExtendedMenu modify = new ExtendedMenu();
                modify.menuList      = db.Menus.ToList();
                modify.menuTableList = new List <Menu>();

                try
                {
                    // Checkbox is the attribute name.
                    // Returns values of the boxes that have been checked as csv.
                    string[] checkboxes = form["checkbox"].Split(',');
                    if (checkboxes.Length != 1) // More than 1 checkbox selected.
                    {
                        TempData["Message"] = "Error: Can only select one from table to modify.";
                        return(View("MenuMaintenance", modify));
                    }
                    else
                    {
                        int iMenuID = int.Parse(checkboxes[0]);
                        modify.menu     = db.Menus.SingleOrDefault(x => x.menuID == iMenuID);
                        modify.menuList = db.Menus.SqlQuery("SELECT * FROM Menu m1 INNER JOIN Menu m2 ON m1.menuID = m2.menuID WHERE m1.menuNo LIKE '00%' AND LEN(m2.MenuNo) = 3").ToList();

                        if (modify.menu.menuNo.Length > 3)
                        {
                            modify.subMenuNo = modify.menu.menuNo.Remove(3);
                        }
                        return(View("ModifyMenu", modify));
                    }
                }
                catch (Exception e) // Catch exception when no item from table was selected.
                {
                    TempData["Message"] = "Error: No item was selected from table.";
                    return(View("MenuMaintenance", modify));
                }
            }
        }
コード例 #25
0
        public ActionResult QueryMenu(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                var menuID   = form["selectMenu"];
                var title    = form["selectTitle"];
                var linkType = form["selectLinkType"];
                // Status is of type string from front-end.
                // bStatus is used to check database.
                var status = form["selectStatus"];

                var queryMenu = db.Menus.AsQueryable();
                if (menuID != "")
                {
                    int iMenuID = int.Parse(menuID);
                    queryMenu = queryMenu.Where(x => x.menuID == iMenuID);
                }
                if (title != "")
                {
                    queryMenu = queryMenu.Where(x => x.title == title);
                }
                if (linkType != "")
                {
                    queryMenu = queryMenu.Where(x => x.linkType == linkType);
                }
                if (status != "")
                {
                    bool bStatus = bool.Parse(status);
                    queryMenu = queryMenu.Where(x => x.status == bStatus);
                }

                ExtendedMenu query = new ExtendedMenu();
                query.menuList      = db.Menus.ToList();
                query.menuTableList = queryMenu.ToList();

                return(View("MenuMaintenance", query));
            }
        }
コード例 #26
0
        public ActionResult ModifyUser(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                ExtendedUser viewModel = new ExtendedUser();
                viewModel.userList      = db.Users.ToList();
                viewModel.roleList      = db.Roles.Where(x => x.status == true).ToList();
                viewModel.userTableList = new List <User>();

                try
                {
                    // Checkbox is the attribute name.
                    // Returns values of the boxes that have been checked as csv.
                    string[] checkboxes = form["checkbox"].Split(',');

                    if (checkboxes.Length != 1) // More than 1 checkbox selected.
                    {
                        TempData["Message"] = "Error: Can only select one from table to modify.";
                        return(View("UserMaintenance", viewModel));
                    }
                    else
                    {
                        int          iSelectUser = int.Parse(checkboxes[0]);
                        ExtendedUser user        = new ExtendedUser();
                        user.user     = db.Users.SingleOrDefault(x => x.userID == iSelectUser);
                        user.roleList = db.Roles.Where(x => x.status == true).ToList();
                        return(View("ModifyUser", user));
                    }
                }
                catch (Exception e) // Catch exception when no item from table was selected.
                {
                    TempData["Message"] = "Error: No item was selected from table.";
                    return(View("UserMaintenance", viewModel));
                }
            }
        }
コード例 #27
0
        public ActionResult AddMenu(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                var level    = form["selectLinkType"];
                var title    = form["title"];
                var linkType = form["selectLinkType"];
                var subMenu  = form["selectSubMenu"];
                var linkUrl  = form["linkUrl"];
                var status   = form["selectStatus"];

                Menu menu = new Menu();
                // Determine the new menu number.
                if (linkType == "1")
                {
                    menu.linkType = "Menu";
                    // Get new main menu number.
                    menu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]);
                }

                if (linkType == "2")
                {
                    if (subMenu != null)
                    {
                        menu.linkType = "Program";
                        // Get new sub menu number.
                        menu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]);
                    }
                    else
                    {
                        TempData["Message"] = "Error: Sub-menu category was not selected.";
                        return(View("MenuMaintenance", "SystemSetup"));
                    }
                }

                menu.level = int.Parse(level);
                menu.title = title;

                if (linkUrl != null) // Optional
                {
                    menu.linkUrl = linkUrl;
                }
                else
                {
                    menu.linkUrl = "";
                }

                menu.status = false;
                if (status == "1")
                {
                    menu.status = true;
                }
                db.Menus.Add(menu);
                db.SaveChanges();

                ExtendedMenu menus = new ExtendedMenu();
                menus.menuList      = db.Menus.ToList();
                menus.menuTableList = new List <Menu>();
                return(View("MenuMaintenance", menus));
            }
        }
コード例 #28
0
        // To modify and update the menu table.
        public ActionResult ModifyMenuDatabase(FormCollection form)
        {
            using (Trainee15Entities db = new Trainee15Entities())
            {
                // Menu object to store and update database.
                Menu         updateMenu = new Menu();
                ExtendedMenu menus      = new ExtendedMenu();

                int menuID = int.Parse(form["menuID"]);
                // Get data from database and assign it to object.
                updateMenu = db.Menus.SingleOrDefault(x => x.menuID == menuID);

                if (form["selectLinkType"] == "1")     //Main Menu
                {
                    if (updateMenu.linkType != "Menu") // Checks if menu isn't already a menu.
                    {
                        updateMenu.linkType = "Menu";
                        // Update menu number if changed from sub menu to main menu.
                        updateMenu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]);
                    }
                }
                if (form["selectLinkType"] == "2") // Sub Menu
                {
                    if (form["selectSubMenu"] != null)
                    {
                        if (updateMenu.linkType != "Program") // Checks if menu isn't already sub menu.
                        {
                            updateMenu.linkType = "Program";
                            // Update menu number if changed from main menu to sub menu.
                            updateMenu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]);
                        }
                        else // If the menu is already a program.
                        {
                            // Compare the menu category from database to the selected menu category.
                            if (string.CompareOrdinal(updateMenu.menuNo, 0, form["selectSubMenu"], 0, 3) != 0)
                            {
                                // Update menu number if menu changed to a different menu category.
                                updateMenu.menuNo = MenuNo(form["selectLinkType"], form["selectSubMenu"]);
                            }
                        }
                    }
                    else
                    {
                        menus.menuList      = db.Menus.ToList();
                        menus.menuTableList = new List <Menu>();
                        TempData["Message"] = "Error: Sub-menu category was not selected.";
                        return(View("MenuMaintenance", menus));
                    }
                }

                updateMenu.level = int.Parse(form["selectLinkType"]);
                updateMenu.title = form["title"];
                if (form["linkUrl"] != null) // Optional
                {
                    updateMenu.linkUrl = form["linkUrl"];
                }
                else
                {
                    updateMenu.linkUrl = "";
                }
                updateMenu.status = false;
                if (form["selectStatus"] == "1")
                {
                    updateMenu.status = true;
                }
                db.SaveChanges();

                menus.menuList      = db.Menus.ToList();
                menus.menuTableList = new List <Menu>();
                TempData["Message"] = "Menu updated.";
                return(View("MenuMaintenance", menus));
            }
        }