Exemple #1
0
        public ActionResult CreateProfile(ProfileFormViewModel viewModel)
        {
            // Ensure form validation
            if (!ModelState.IsValid)
            {
                return(View("ProfileForm", viewModel));
            }

            var userId = User.Identity.GetUserId();

            // New student object takes the information from the form
            var student = new Student
            {
                Name          = viewModel.Name,
                Alias         = viewModel.Alias,
                Ability       = viewModel.Ability,
                Grade         = viewModel.Grade,
                Origin        = viewModel.Origin,
                StudentUserId = userId
            };

            // Add to database
            _context.Students.Add(student);
            _context.SaveChanges();

            return(RedirectToAction("StudentInfo", "Student"));
        }
Exemple #2
0
        public ActionResult CreateProfile()
        {
            var viewModel = new ProfileFormViewModel
            {
                Heading = "Add Your Profile"
            };

            return(View("ProfileForm", viewModel));
        }
Exemple #3
0
        public async Task <IActionResult> Edit(ProfileFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.FindByIdAsync(model.Id);

            if (user == null)
            {
                return(NotFound());
            }

            /* Important*/
            /* لازم اتأكد ان اليوزر لما هيغير الايميل لو الايميل دة موجود اصلا مع يوزر تاني يبقى اديله ايرور بسيط و افهمه ان الايميل دة مع يوزر تاني  */

            var userWithSameEmail = await _userManager.FindByEmailAsync(model.Email);

            if (userWithSameEmail != null && userWithSameEmail.Id != model.Id)
            {
                ModelState.AddModelError("Email", "Sorry this emails is assigned to another user!");
                return(View(model));
            }

            var userWithSameUserName = await _userManager.FindByNameAsync(model.UserName);

            if (userWithSameUserName != null && userWithSameUserName.Id != model.Id)
            {
                ModelState.AddModelError("UserName", "Sorry this username is assigned to another user");
                return(View(model));
            }



            user.Id        = model.Id;
            user.Email     = model.Email;
            user.UserName  = model.UserName;
            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;

            await _userManager.UpdateAsync(user);

            return(RedirectToAction(nameof(Index)));
        }
Exemple #4
0
        public ActionResult EditProfile(int id)
        {
            var userId = User.Identity.GetUserId();

            var student = _context.Students.Single(s => s.Id == id && s.StudentUserId == userId);

            var viewModel = new ProfileFormViewModel
            {
                Id      = student.Id,
                Heading = "Edit Your Profile",
                Name    = student.Name,
                Alias   = student.Alias,
                Ability = student.Ability,
                Grade   = student.Grade,
                Origin  = student.Origin
            };

            return(View("ProfileForm", viewModel));
        }
Exemple #5
0
        public async Task <IActionResult> Edit(string userId)
        {
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(NotFound());
            }

            var viewModel = new ProfileFormViewModel
            {
                Id        = userId,
                FirstName = user.FirstName,
                LastName  = user.LastName,
                UserName  = user.UserName,
                Email     = user.Email,
            };

            return(View(viewModel));
        }
Exemple #6
0
        public ActionResult EditProfile(ProfileFormViewModel viewModel)
        {
            // Ensure form validation
            if (!ModelState.IsValid)
            {
                return(View("ProfileForm", viewModel));
            }

            var userId = User.Identity.GetUserId();

            // Current student is selected for update
            var student = _context.Students.Single(s => s.Id == viewModel.Id && s.StudentUserId == userId);

            // Student properties are updated by the form
            student.Name    = viewModel.Name;
            student.Alias   = viewModel.Alias;
            student.Ability = viewModel.Ability;
            student.Grade   = viewModel.Grade;
            student.Origin  = viewModel.Origin;

            _context.SaveChanges();

            return(RedirectToAction("StudentInfo", "Student"));
        }