コード例 #1
0
        // GET: Talents/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var talent = await _context.Talent
                         .Include(t => t.ApplicationUser)
                         .Include(t => t.Skills)
                         .FirstOrDefaultAsync(t => t.Id == id);

            var editedTalent = new NewTalentAndSkills()
            {
                Id              = talent.Id,
                Active          = talent.Active,
                ApplicationUser = talent.ApplicationUser,
                Wage            = talent.Wage,
                Biography       = talent.Biography,
                SkillList       = new List <string>()
            };

            foreach (TalentSkill skill in talent.Skills)
            {
                editedTalent.SkillList.Add(skill.Skill);
            }

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

            return(View(editedTalent));
        }
コード例 #2
0
        public async Task <IActionResult> CreatingTalent([Bind("Id,Biography,Wage,SkillList")] NewTalentAndSkills model)
        {
            ModelState.Remove("ApplicationUserId");
            ModelState.Remove("ApplicationUser");
            var user = await GetCurrentUserAsync();

            if (ModelState.IsValid)
            {
                Talent talent = new Talent()
                {
                    Id                = model.Id,
                    Active            = true,
                    Biography         = model.Biography,
                    Wage              = model.Wage,
                    ApplicationUserId = user.Id
                };

                _context.Add(talent);
                await _context.SaveChangesAsync();

                if (model.SkillList != null)
                {
                    bool skillsProcessed = await AddSkills(model.SkillList, user.Id);
                }

                return(RedirectToAction(nameof(AllTalents)));
            }

            return(View());
        }
コード例 #3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Active,Biography,Wage,SkillList")] NewTalentAndSkills editedTalent)
        {
            ModelState.Remove("ApplicationUserId");
            ModelState.Remove("ApplicationUser");
            var user = await GetCurrentUserAsync();

            if (id != editedTalent.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var skills = await _context.TalentSkill
                                 .Where(c => c.TalentID == editedTalent.Id)
                                 .ToListAsync();

                    foreach (TalentSkill skill in skills)
                    {
                        _context.TalentSkill.Remove(skill);
                        await _context.SaveChangesAsync();
                    }

                    if (editedTalent.SkillList != null)
                    {
                        var skillsMade = await AddSkills(editedTalent.SkillList, user.Id);
                    }

                    var talent = new Talent()
                    {
                        Id = id,
                        ApplicationUserId = user.Id,
                        Biography         = editedTalent.Biography,
                        Wage   = editedTalent.Wage,
                        Active = editedTalent.Active
                    };

                    _context.Update(talent);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TalentExists(editedTalent.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(AllTalents)));
            }
            return(View(editedTalent));
        }