// GET: /Students/
        public ActionResult Index()
        {
            try
            {
                dbTIREntities db = new dbTIREntities();
                SiteUser siteUser = ((SiteUser)Session["SiteUser"]);
                StudentService studentService = new StudentService(siteUser, db);
                SchoolService schoolService = new SchoolService(siteUser, db);
                ModelServices modelService = new ModelServices();
                string currentSchoolYear = schoolService.GetCurrentSchoolYear();
                ViewBag.DistrictDesc = siteUser.Districts[0].Name;
                int schoolYearId = modelService.SchoolYearId();
                ViewBag.SchoolId = modelService.DropDownDataSchool("", siteUser.EdsUserId, schoolYearId, true);
                ViewBag.AllowEdit = HelperService.AllowUiEdits(siteUser.RoleDesc, "STUDENT");
                //ViewBag.SchoolYear = HelperService.SchoolYearDescription(db);
                ViewBag.SchoolYearList = schoolService.DropDownDataSchoolYear(currentSchoolYear);
                ViewBag.AllowEdit = HelperService.AllowUiEdits(siteUser.RoleDesc, "STUDENT");
                ViewBag.SchoolYear = currentSchoolYear;

                return View(new SiteModels()
                {
                    Students = studentService.GetViewData(currentSchoolYear, "", "")
                });
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }
 public ActionResult Index()
 {
     try
     {
         db = new dbTIREntities();
         siteUser = ((SiteUser)Session["SiteUser"]);
         userService = new UserService(siteUser, db);
         schoolService = new SchoolService(siteUser, db);
         modelService = new ModelServices();
         int schoolYearId = modelService.SchoolYearId();
         string currentSchoolYear = schoolService.GetCurrentSchoolYear();
         ViewBag.SchoolYearList = schoolService.DropDownDataSchoolYear(currentSchoolYear);
         FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, schoolYearId);
         return View(userService.GetViewData(currentSchoolYear, "", ""));
     }
     catch (Exception ex)
     {
         Logging log = new Logging();
         log.LogException(ex);
         return View("GeneralError");
         //throw;
     }
 }
        public async Task<ActionResult> Create(tblUserExt tblUserExtended)
        {
            try
            {
                db = new dbTIREntities();
                modelService = new ModelServices();
                siteUser = ((SiteUser)Session["SiteUser"]);
                userService = new UserService(siteUser, db);
                schoolService = new SchoolService(siteUser, db);

                int userAssignedDistrict = siteUser.Districts[0].Id;
                string currentSchoolYear = schoolService.GetCurrentSchoolYear();

                if (ModelState.IsValid)
                {
                    if (tblUserExtended.SelectedSchools != null && tblUserExtended.SelectedSchools.Count() > 0)
                    {
                        var context = new Models.ApplicationDbContext();
                        var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

                        // 1. Create ASPNET user
                        string userName = tblUserExtended.UserName;
                        string password = tblUserExtended.Password;

                        var isPasswordValid = password != null && password.Length >= 6 ? true : false;
                        var isUserNameExist = userManager.FindByName(userName);
                        bool isEmailAddressExist = db.tblUsers.Where(x => x.UserEmail == tblUserExtended.UserEmail).Count() > 0 ? true : false;
                        bool isStateIdExist = db.tblUsers.Where(x => x.StateId == tblUserExtended.StateId).Count() > 0 ? true : false;

                        if ((isUserNameExist == null) && (!isEmailAddressExist) && (!isStateIdExist) && (isPasswordValid))
                        {
                            var user = new ApplicationUser() { UserName = userName };
                            var result = await userManager.CreateAsync(user, password);
                            if (result.Succeeded)
                            {
                                // 2. Create EDS user
                                ApplicationUser newAspNetUser = userManager.FindByName(userName);
                                if (newAspNetUser != null)
                                {
                                    userService.CreateEdsUser(newAspNetUser.Id, tblUserExtended);
                                }
                            }
                            else
                            {
                                throw new Exception(String.Format("ERROR: {0}", result.Errors));
                            }
                            return RedirectToAction("Index");
                        }
                        else
                        {
                            if (isUserNameExist != null)
                                ModelState.AddModelError("UserName", "Duplicate name - please choose a unique name.");
                            if (isEmailAddressExist)
                                ModelState.AddModelError("UserEmail", "Duplicate email - please choose a unique email.");
                            if (isStateIdExist)
                                ModelState.AddModelError("StateId", "Duplicate state id - please choose a unique state.");
                            if (!isPasswordValid)
                                ModelState.AddModelError("Password", "Please enter password at least 6 characters.");
                        }
                    }
                    else
                    {
                        ViewBag.SchoolMessage = "Required";
                    }
                }

                tblUserExtended.Schools = userService.GetSelectedSchoolCheckBoxes(tblUserExtended);
                ViewBag.RoleId = new SelectList(modelService.GetRolesForRole((int)(siteUser.Role)), "RoleId", "RoleDesc", tblUserExtended.RoleId);
                FillViewBagValues(siteUser.Districts[0].Name, string.Empty, siteUser.RoleDesc, tblUserExtended.SchoolYearId);
                return View(tblUserExtended);
            }
            catch (Exception ex)
            {
                Logging log = new Logging();
                log.LogException(ex);
                return View("GeneralError");
            }
        }