private SystemMessagePageState GetPageState()
        {
            try
            {
                SystemMessagePageState pagestate = new SystemMessagePageState();

                // Initialize the session values if they don't exist - need to do this the first time controller is hit
                if (Session["SystemMessagePageState"] == null)
                {
                    pagestate.SystemMessageTitle      = String.Empty;
                    pagestate.SystemMessageBody       = String.Empty;
                    pagestate.SortBy                  = "SystemMessageTitle";
                    pagestate.AscDesc                 = "Ascending";
                    pagestate.PageNumber              = 1;
                    Session["SystemMessagePageState"] = pagestate;
                }
                else
                {
                    pagestate = (SystemMessagePageState)Session["SystemMessagePageState"];
                }
                return(pagestate);
            }
            catch { return(new SystemMessagePageState()); }
        }
        //
        // GET: /SystemMessage/

        public ActionResult Index()
        {
            try
            {
                if (Session["UserAccountID"] == null)
                {
                    return(RedirectToAction("Validate", "Login"));
                }
                User user = (User)Session["User"];
                ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"]));
                if (user.IsAdmin)
                {
                    ViewData["txtIsAdmin"] = "true";
                }
                else
                {
                    throw new Exception("You are not authorized to access this page.");
                }

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

                // Get the account id
                int accountid = 0;
                if (Session["UserAccountID"] != null)
                {
                    accountid = Convert.ToInt32(Session["UserAccountID"]);
                }

                // Set and save the page state to the submitted form values if any values are passed
                if (Request.Form["lstAscDesc"] != null)
                {
                    pagestate.SystemMessageTitle = Request.Form["txtTitle"].ToString().Trim();
                    pagestate.SystemMessageBody  = Request.Form["txtBody"].ToString().Trim();
                    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["SystemMessageTitle"] = pagestate.SystemMessageTitle;
                ViewData["SystemMessageBody"]  = pagestate.SystemMessageBody;
                ViewData["SortBy"]             = pagestate.SortBy;
                ViewData["SortByList"]         = new SelectList(BuildSortByList(), "Value", "Text", pagestate.SortBy);
                ViewData["AscDescList"]        = new SelectList(BuildAscDescList(), "Value", "Text", pagestate.AscDesc);

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

                // Get a Count of all filtered records
                int recordcount = repository.GetSystemMessageRecordCount(pagestate.SystemMessageBody, pagestate.SystemMessageBody);

                // 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);

                ViewResult result = View(repository.GetSystemMessagePage(pagestate.SystemMessageTitle, pagestate.SystemMessageBody, pagestate.SortBy, isdescending, pagestate.PageNumber, pagecount));
                return(result);
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("SystemMessage", "Index", ex.Message);
                return(RedirectToAction("Index", "ApplicationError"));
            }
        }
 private void SavePageState(SystemMessagePageState pagestate)
 {
     Session["SystemMessagePageState"] = pagestate;
 }