Exemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, [Bind("PersonalId,Name,DataOfBirthday,PhoneNumber,Address,ProfessionId,ContractId,Professions")] Personal personal)
        {
            if (id != personal.PersonalId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var professions = personal.Professions.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    var teamsToRemove = _context.PersonalProfession.Where(t => t.PersonalId == personal.PersonalId).ToList();

                    foreach (var tToRemove in teamsToRemove)
                    {
                        _context.PersonalProfession.Remove(tToRemove);
                    }

                    _context.SaveChanges();


                    foreach (var t in professions)
                    {
                        var professionDb = _context.Profession.FirstOrDefault(s => s.ProfessionName == t);
                        if (professionDb != null)
                        {
                            _context.Add(new PersonalProfession {
                                Profession = professionDb, Personal = personal
                            });
                        }
                    }

                    _context.Update(personal);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PersonalExists(personal.PersonalId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ContractId"] = new SelectList(_context.Contract, "ContractId", "ContractId", personal.ContractId);
            return(View(personal));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, [Bind("CoachId,Name,DateOfBirhday,Address,PhoneNumber,PositionInTeam,TeamCoach,Teams,ContractId")] Coach coach)
        {
            if (id != coach.CoachId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var teams = coach.Teams.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    var teamsToRemove = _context.TeamCoach.Where(t => t.CoachId == coach.CoachId).ToList();

                    foreach (var tToRemove in teamsToRemove)
                    {
                        _context.TeamCoach.Remove(tToRemove);
                    }

                    _context.SaveChanges();


                    foreach (var t in teams)
                    {
                        var teamDb = _context.Team.FirstOrDefault(s => s.NameTeam == t);
                        if (teamDb != null)
                        {
                            _context.Add(new TeamCoach {
                                Team = teamDb, Coach = coach
                            });
                        }
                    }

                    _context.Update(coach);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CoachExists(coach.CoachId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ContractId"] = new SelectList(_context.Contract, "ContractId", "ContractId", coach.ContractId);
            return(View(coach));
        }