示例#1
0
        private UserPageState GetPageState()
        {
            try
            {
                UserPageState pagestate = new UserPageState();


                // Initialize the session values if they don't exist - need to do this the first time controller is hit
                if (Session["UserPageState"] == null)
                {
                    int accountid = 0;
                    pagestate.AccountID       = accountid;
                    pagestate.Username        = String.Empty;
                    pagestate.IncludeInactive = false;
                    pagestate.SortBy          = "Username";
                    pagestate.AscDesc         = "Ascending";
                    pagestate.PageNumber      = 1;
                    Session["UserPageState"]  = pagestate;
                }
                else
                {
                    pagestate = (UserPageState)Session["UserPageState"];
                }
                return(pagestate);
            }
            catch { return(new UserPageState()); }
        }
示例#2
0
 private void SavePageState(UserPageState pagestate)
 {
     Session["UserPageState"] = pagestate;
 }
示例#3
0
        //
        // GET: /User/

        public ActionResult Index()
        {
            try
            {
                if (Session["UserAccountID"] == null)
                {
                    return(RedirectToAction("Validate", "Login"));
                }
                User currentuser = (User)Session["User"];
                ViewData["LoginInfo"] = "<b>User:</b> " + currentuser.Username + "&nbsp; &nbsp; &nbsp;<b>Account:</b> " + Session["UserAccountName"];
                if (currentuser.IsAdmin)
                {
                    ViewData["txtIsAdmin"] = "true";
                }
                else
                {
                    throw new Exception("You are not authorized to access this page.");
                }

                // Initialize or get the page state using session
                UserPageState pagestate = GetPageState();

                // Set and save the page state to the submitted form values if any values are passed
                if (Request.Form["lstAscDesc"] != null)
                {
                    pagestate.AccountID = Convert.ToInt32(Request.Form["lstAccount"]);
                    pagestate.Username  = Request.Form["txtUsername"].ToString().Trim();
                    if (Request.Form["chkIncludeInactive"].ToLower().StartsWith("true"))
                    {
                        pagestate.IncludeInactive = true;
                    }
                    else
                    {
                        pagestate.IncludeInactive = false;
                    }
                    pagestate.SortBy     = Request.Form["lstSortBy"].ToString().Trim();
                    pagestate.AscDesc    = Request.Form["lstAscDesc"].ToString().Trim();
                    pagestate.PageNumber = Convert.ToInt32(Request.Form["txtPageNumber"].ToString().Trim());
                    SavePageState(pagestate);
                }

                // Add the session values to the view data so they can be populated in the form
                ViewData["AccountID"]       = pagestate.AccountID;
                ViewData["Username"]        = pagestate.Username;
                ViewData["IncludeInactive"] = pagestate.IncludeInactive;
                ViewData["SortBy"]          = pagestate.SortBy;
                ViewData["SortByList"]      = new SelectList(BuildSortByList(), "Value", "Text", pagestate.SortBy);
                ViewData["AscDescList"]     = new SelectList(BuildAscDescList(), "Value", "Text", pagestate.AscDesc);
                ViewData["AccountList"]     = new SelectList(BuildAccountList(true), "Value", "Text", pagestate.AccountID);

                // Determine asc/desc
                bool isdescending = false;
                if (pagestate.AscDesc.ToLower().StartsWith("d"))
                {
                    isdescending = true;
                }

                // Get a Count of all filtered records
                int recordcount = repository.GetUserRecordCount(pagestate.AccountID, pagestate.Username, pagestate.IncludeInactive);

                // Determine the page count
                int pagecount = 1;
                if (recordcount > 0)
                {
                    pagecount = recordcount / Constants.PageSize;
                    if (recordcount % Constants.PageSize != 0) // Add a page if there are more records
                    {
                        pagecount = pagecount + 1;
                    }
                }

                // Make sure the current page is not greater than the page count
                if (pagestate.PageNumber > pagecount)
                {
                    pagestate.PageNumber = pagecount;
                    SavePageState(pagestate);
                }

                // Set the page number and account in viewdata
                ViewData["PageNumber"]  = Convert.ToString(pagestate.PageNumber);
                ViewData["PageCount"]   = Convert.ToString(pagecount);
                ViewData["RecordCount"] = Convert.ToString(recordcount);

                // We need to add the account name
                IEnumerable <User> users     = repository.GetUserPage(pagestate.AccountID, pagestate.Username, pagestate.IncludeInactive, pagestate.SortBy, isdescending, pagestate.PageNumber, pagecount);
                List <UserView>    userviews = new List <UserView>();
                IAccountRepository acctrep   = new EntityAccountRepository();
                foreach (User user in users)
                {
                    UserView userview = new UserView();
                    userview.UserID    = user.UserID;
                    userview.AccountID = user.AccountID;
                    Account acct = acctrep.GetAccount(user.AccountID);
                    userview.AccountName  = acct.AccountName;
                    userview.Username     = user.Username;
                    userview.FirstName    = user.FirstName;
                    userview.LastName     = user.LastName;
                    userview.EmailAddress = user.EmailAddress;
                    userview.IsAdmin      = user.IsAdmin;
                    userview.IsActive     = user.IsActive;

                    userviews.Add(userview);
                }

                ViewResult result = View(userviews);
                result.ViewName = "Index";
                return(result);
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("User", "Index", ex.Message);
                return(RedirectToAction("Index", "ApplicationError"));
            }
        }