コード例 #1
0
        public ActionResult NewStudent(UserViewModel user)
        {
            var universities = _commonBusinessLogic.GetUniversitiesList();

            if (ModelState.IsValid)
            {
                if (string.IsNullOrEmpty(user.StudentId))
                {
                    ModelState.AddModelError("StudentId", "Student ID is required");
                    universities.Insert(0, new University()
                    {
                        Name = "Select university", Id = 0
                    });
                    user.Universities = new SelectList(universities.Select(x => new SelectListItem()
                    {
                        Text = x.Name, Value = x.Id.ToString()
                    }), "Value", "Text");
                    return(View(user));
                }
                try
                {
                    _adminBusinessLogic.CreateNewUser(user, UserRoles.Student);
                }
                catch (FormValidationException e)
                {
                    e.ValidationToModelState(ModelState);
                    universities.Insert(0, new University()
                    {
                        Name = "Select university", Id = 0
                    });
                    user.Universities = new SelectList(universities.Select(x => new SelectListItem()
                    {
                        Text = x.Name, Value = x.Id.ToString()
                    }), "Value", "Text");
                    return(View(user));
                }

                return(RedirectToAction("Students", "Coordinator"));
            }
            universities.Insert(0, new University()
            {
                Name = "Select university", Id = 0
            });
            user.Universities = new SelectList(universities.Select(x => new SelectListItem()
            {
                Text = x.Name, Value = x.Id.ToString()
            }), "Value", "Text");
            return(View(user));
        }