// // 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 + " <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"); } }
private List<SelectListItem> BuildAccountList(bool addAllItem) { // Build the player group list List<SelectListItem> acctitems = new List<SelectListItem>(); // Add an 'All' item at the top if (addAllItem) { SelectListItem all = new SelectListItem(); all.Text = "All Accounts"; all.Value = "0"; acctitems.Add(all); } IAccountRepository acctrep = new EntityAccountRepository(); IEnumerable<Account> accts = acctrep.GetAllAccounts(); foreach (Account acct in accts) { SelectListItem item = new SelectListItem(); item.Text = acct.AccountName; item.Value = acct.AccountID.ToString(); acctitems.Add(item); } return acctitems; }
private List<SelectListItem> BuildAccountList(int accountid) { // Build the account list List<SelectListItem> accountitems = new List<SelectListItem>(); IAccountRepository acctrep = new EntityAccountRepository(); IEnumerable<Account> accounts = acctrep.GetAllAccounts(); foreach (Account account in accounts) { SelectListItem item = new SelectListItem(); item.Text = account.AccountName; item.Value = account.AccountID.ToString(); accountitems.Add(item); } return accountitems; }