Exemplo n.º 1
0
        // GET: Admin/Create
        public ActionResult Create()
        {
            int userID = Convert.ToInt32(Session["UserID"] != null ? Session["UserID"].ToString() : "0");   // Convert session user id to integer for comparison and prevent from NULL

            var usr = from u in db.users
                      select u.UserID;                                       // Get current user IDs in database

            TempData["usrMax"] = Convert.ToInt32(usr.Max().ToString()) + 1;  // Put next user id into temp data

            var Role = db.roles.Select(r => new SelectListItem               // Create role selection list
            {
                Text  = r.RoleName,
                Value = r.RoleID.ToString()
            });

            ViewBag.UserRoleID = new SelectList(Role.OrderBy(r => r.Text), "Value", "Text");      // Pass role selection list

            ViewBag.userName = (from u in db.users
                                where (u.UserID == userID)
                                select u.UserFName).FirstOrDefault().ToString(); // Passing user first name to view

            UserAcctViewModels uaModel = new UserAcctViewModels();               // Create a new user account view model

            return(View(uaModel));
        }
Exemplo n.º 2
0
        public ActionResult Create(UserAcctViewModels uaModel, string UserRoleID)
        {
            int userID = Convert.ToInt32(Session["UserID"].ToString());                                        // Get session user id

            uaModel.User.UserRoleID = Convert.ToInt32(UserRoleID != null ? UserRoleID.ToString() : "1");       // Set user role id
            int acctID = Convert.ToInt32(TempData["usrMax"] != null ? TempData["usrMax"].ToString() : "1000"); // Set account id

            account acct = new account()                                                                       // Create a new account
            {
                AcctID     = acctID,
                AcctLName  = uaModel.AcctLName,
                AcctPasswd = uaModel.AcctPasswd
            };

            if (ModelState.IsValid)         // Add user and account
            {
                db.users.Add(uaModel.User);
                db.SaveChanges();
                db.accounts.Add(acct);
                db.SaveChanges();
                return(RedirectToAction("Index", new { sucMsg = "New user created" }));     // Go back to list and display successful message
            }

            // If the model was invalid, display and do it over again
            var usr = from u in db.users
                      select u.UserID;                                      // Get current user IDs in database

            TempData["usrMax"] = Convert.ToInt32(usr.Max().ToString()) + 1; // Put next user id into temp data

            var Role = db.roles.Select(r => new SelectListItem              // Create role selection list
            {
                Text  = r.RoleName,
                Value = r.RoleID.ToString()
            });

            ViewBag.UserRoleID = new SelectList(Role.OrderBy(r => r.Text), "Value", "Text");      // Pass role selection list

            ViewBag.userName = (from u in db.users
                                where (u.UserID == userID)
                                select u.UserFName).FirstOrDefault().ToString(); // Passing user first name to view

            UserAcctViewModels uaModelA = new UserAcctViewModels();              // Create a new user account view model

            return(View(uaModelA));
        }