コード例 #1
0
        // Method to update AlumTechs
        private void UpdateAlumTechs(int[] selectedTechs, Alum alumToUpdate)
        {
            if (selectedTechs == null)
            {
                var unknown = (from t in _context.Tech
                               where t.Name.Equals("(Unknown)")
                               select t.Id).ToArray();
                selectedTechs = unknown;
            }

            var selectedTechsHS = new HashSet <int>(selectedTechs);
            var alumTechs       = new HashSet <int>
                                      (alumToUpdate.AlumTech.Select(c => c.Tech.Id));

            foreach (var tech in _context.Tech)
            {
                if (selectedTechsHS.Contains(tech.Id))
                {
                    if (!alumTechs.Contains(tech.Id))
                    {
                        alumToUpdate.AlumTech.Add(new AlumTech {
                            AlumId = alumToUpdate.Id, TechId = tech.Id
                        });
                    }
                }
                else
                {
                    if (alumTechs.Contains(tech.Id))
                    {
                        AlumTech techToRemove = alumToUpdate.AlumTech.SingleOrDefault(c => c.TechId == tech.Id);
                        _context.Remove(techToRemove);
                    }
                }
            }
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,CohortId,Address,Address2,City,State,ZipCode,Phone,Email,GitHub,LinkedIn,Slack")] Alum alum)
        {
            if (ModelState.IsValid)
            {
                _context.Add(alum);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CohortId"] = new SelectList(_context.Cohort, "Id", alum.CohortId);
            return(View(alum));
        }
コード例 #3
0
        // Adding viewmodel to display multiple Tech checkboxes
        private List <AssignedTechData> PopulateAlumTechData(Alum alum)
        {
            var allTechs  = _context.Tech;
            var alumTechs = new HashSet <int>(alum.AlumTech.Select(a => a.TechId));
            var viewModel = new List <AssignedTechData>();

            foreach (var tech in allTechs)
            {
                viewModel.Add(new AssignedTechData
                {
                    TechId   = tech.Id,
                    TechName = tech.Name,
                    Assigned = alumTechs.Contains(tech.Id)
                });
            }
            return(viewModel);
        }
コード例 #4
0
        public async Task <IActionResult> Edit(int id, int[] selectedTechs, [Bind("Id,FirstName,LastName,CohortId,Address,City,State,ZipCode,Phone,Email,GitHub,LinkedIn,Slack")] Alum alum)
        {
            if (id != alum.Id)
            {
                return(NotFound());
            }

            var alumToUpdate = await _context.Alum
                               .Include(a => a.Cohort)
                               .Include(a => a.AlumTech).ThenInclude(at => at.Tech)
                               .SingleOrDefaultAsync(m => m.Id == id);

            List <string> cohortList = _context.Cohort.Select(c => c.Id).ToList();

            if (await TryUpdateModelAsync <Alum>(
                    alumToUpdate,
                    "",
                    a => a.Id, a => a.FirstName, a => a.LastName, a => a.CohortId, a => a.Address, a => a.City, a => a.State, a => a.ZipCode, a => a.Phone, a => a.Email, a => a.GitHub, a => a.LinkedIn, a => a.Slack, a => a.AlumTech))
            {
                UpdateAlumTechs(selectedTechs, alumToUpdate);
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException /* ex */)
                {
                    //Log the error (uncomment ex variable name and write a log.)
                    ModelState.AddModelError("", "Unable to save changes. " +
                                             "Try again, and if the problem persists, " +
                                             "see your system administrator.");
                }
                return(RedirectToAction("Details", new { id }));
            }
            ViewData["CohortList"] = cohortList;
            UpdateAlumTechs(selectedTechs, alumToUpdate);
            PopulateAlumTechData(alumToUpdate);
            return(View(alumToUpdate));
        }