public async Task <IActionResult> Create([Bind("StudentID,Name,JoinDate,ActiveStudent,Address")] Student student)
        {
            if (ModelState.IsValid)
            {
                _context.Add(student);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }
Example #2
0
        public async Task <IActionResult> Create([Bind("AllergiesID,StudentID,Allergy")] Allergies allergies)
        {
            if (ModelState.IsValid)
            {
                _context.Add(allergies);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["StudentID"] = new SelectList(_context.Student, "StudentID", "Name", allergies.StudentID);
            return(View(allergies));
        }
        public async Task <IActionResult> Create([Bind("StudentID,Name,JoinDate,ActiveStudent,Address")] Student student, string[] selectedAllergies)
        {
            if (ModelState.IsValid)
            {
                _context.Add(student);
                await _context.SaveChangesAsync();

                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.AddRange(allergies);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    Console.WriteLine("selectedAllergies empty!");
                }


                return(RedirectToAction(nameof(Index)));
            }
            return(View(student));
        }