Exemple #1
0
        public async Task <IActionResult> Edit(int id, [Bind("AllergiesID,StudentID,Allergy")] Allergies allergies)
        {
            if (id != allergies.AllergiesID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(allergies);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AllergiesExists(allergies.AllergiesID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["StudentID"] = new SelectList(_context.Student, "StudentID", "Name", allergies.StudentID);
            return(View(allergies));
        }
        public async Task <IActionResult> Edit(int id, [Bind("StudentID,Name,JoinDate,ActiveStudent,Address")] Student student)
        {
            if (id != student.StudentID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(student);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StudentExists(student.StudentID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }
        public async Task <IActionResult> Edit(int id, [Bind("StudentID,Name,JoinDate,ActiveStudent,Address")] Student student, string[] selectedAllergies)
        {
            if (id != student.StudentID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(student);
                    await _context.SaveChangesAsync();

                    var test = await _context.Student
                               .Include(a => a.StudentsAllergies)
                               .FirstOrDefaultAsync(m => m.StudentID == id);

                    var currentAllergies = test.StudentsAllergies;

                    Console.WriteLine("currentAllergies:");
                    Console.WriteLine(currentAllergies);
                    Array.ForEach(selectedAllergies, Console.WriteLine);

                    if (selectedAllergies.Length != 0)
                    {
                        var allergies = new List <Allergies>();
                        //Console.WriteLine("selectedAllergies.Length:" + selectedAllergies.Length);
                        //Console.WriteLine("selectedAllergies:");
                        //Array.ForEach(selectedAllergies, Console.WriteLine);
                        for (var i = 0; i < selectedAllergies.Length; i++)
                        {
                            if (String.IsNullOrEmpty(selectedAllergies[i]) == false)
                            {
                                //Console.WriteLine(selectedAllergies[i] + " is NOT empty!");

                                allergies.Add(new Allergies {
                                    StudentID = student.StudentID,
                                    Allergy   = selectedAllergies[i]
                                });
                            }
                            else
                            {
                                //Console.WriteLine(selectedAllergies[i] + " is empty!");
                            }
                        }
                        _context.Allergies.RemoveRange(currentAllergies);
                        _context.AddRange(allergies);
                        await _context.SaveChangesAsync();
                    }
                    else
                    {
                        Console.WriteLine("selectedAllergies empty!");
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StudentExists(student.StudentID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }