コード例 #1
0
        private RegisterModel GenerateRegisterForm(RegisterModel model=null)
        {
            var db = new Context();
            model = model?? new RegisterModel();

            model.States = db.States;
            model.Districts = db.Districts.Where(t => t.State_Id == model.State);
            model.Schools = db.Schools.Where(t => t.District_Id == model.District);

            var result = new List<SelectListItem>();
            foreach (var t in db.Subjects)
            {
                var selectListItem = new SelectListItem
                {
                    Text = t.SubjectName,
                    Value = t.Id.ToString(),
                };
                if(model.SubjectIds != null && model.SubjectIds.Contains(t.Id))
                {
                    selectListItem.Selected = true;
                }
                result.Add(selectListItem);
            }
            model.Subjects = result.ToList();

            result = new List<SelectListItem>();
            foreach (var t in db.Grades)
            {
                var selectListItem = new SelectListItem
                {
                    Text = t.GradeName,
                    Value = t.Id.ToString(),
                };
                if (model.GradeIds != null && model.GradeIds.Contains(t.Id))
                {
                    selectListItem.Selected = true;
                }
                result.Add(selectListItem);
            }
            model.Grades = result.ToList();

            return model;
        }
コード例 #2
0
        public ActionResult Register(RegisterModel model)
        {
            var db = new Context();
            if (ModelState.IsValid)
            {
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { model.Email, model.First, model.Last });
                    WebSecurity.Login(model.UserName, model.Password);

                    var school = db.Schools.FirstOrDefault(t => t.Id == model.School);
                    var user = db.UserProfiles.FirstOrDefault(t => t.UserName == model.UserName);
                    db.UserProfileSchools.Add(new UserProfileSchool { School = school, UserProfile = user });

                    foreach (var gradeId in model.GradeIds)
                    {
                        var userGrade = new UserGrade { UserProfile = user, Grade_Id = gradeId };
                        db.UserGrades.Add(userGrade);
                    }

                    foreach (var subjectId in model.SubjectIds)
                    {
                        var userSubject = new UserSubject { UserProfile = user, Subject_Id = subjectId };
                        db.UserSubjects.Add(userSubject);
                    }
                    db.SaveChanges();
                    return RedirectToAction("TakeATour", "Home", new { Area = "" });
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }
            return View(GenerateRegisterForm(model));
        }