コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,CourseName,TotalCredits")] Course course)
        {
            if (ModelState.IsValid)
            {
                _context.Add(course);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(course));
        }
        public async Task <IActionResult> Create([Bind("Id,StudentId,CourseId,StartDate,EndDate")] Enrolment enrolment)
        {
            if (ModelState.IsValid)
            {
                _context.Add(enrolment);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CourseId"]  = new SelectList(_context.Course, "Id", "Id", enrolment.CourseId);
            ViewData["StudentId"] = new SelectList(_context.Set <Student>(), "Id", "Id", enrolment.StudentId);
            return(View(enrolment));
        }
コード例 #3
0
        public async Task <IActionResult> CreateAsync([Bind("Id,Name,Email,Phone,Password")] Student student)
        {
            if (ModelState.IsValid)
            {
                _context.Add(student);
                _context.SaveChanges();

                var user = new IdentityUser {
                    UserName = student.Email, Email = student.Email
                };
                user.EmailConfirmed = true;
                var result = await _userManager.CreateAsync(user, student.Password);

                if (result.Succeeded)
                {
                    string studentRole = "student";


                    var roleExists = await _roleManager.RoleExistsAsync(studentRole);

                    if (!roleExists)
                    {
                        var roleReuslt = await _roleManager.CreateAsync(new IdentityRole(studentRole));

                        if (roleReuslt.Succeeded)
                        {
                            await _userManager.AddToRoleAsync(user, studentRole);
                        }
                    }
                    else
                    {
                        await _userManager.AddToRoleAsync(user, studentRole);
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }