コード例 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,StudId,AbonId,ActivationDate")] StudentAbonements studentAbonements)
        {
            if (id != studentAbonements.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(studentAbonements);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StudentAbonementsExists(studentAbonements.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AbonId"] = new SelectList(_context.Abonements, "Id", "Id", studentAbonements.AbonId);
            ViewData["StudId"] = new SelectList(_context.Students, "Id", "Id", studentAbonements.StudId);
            return(View(studentAbonements));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,StudId,AbonId,ActivationDate")] StudentAbonements studentAbonements)
        {
            if (ModelState.IsValid)
            {
                _context.Add(studentAbonements);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AbonId"] = new SelectList(_context.Abonements, "Id", "Id", studentAbonements.AbonId);
            ViewData["StudId"] = new SelectList(_context.Students, "Id", "Id", studentAbonements.StudId);
            return(View(studentAbonements));
        }
コード例 #3
0
ファイル: StudentsController.cs プロジェクト: elzayts/IStaTP
        public async Task <IActionResult> Import(IFormFile fileExcel)
        {
            if (ModelState.IsValid)
            {
                if (fileExcel != null)
                {
                    using (var stream = new FileStream(fileExcel.FileName, FileMode.Create))
                    {
                        await fileExcel.CopyToAsync(stream);

                        using (XLWorkbook workBook = new XLWorkbook(stream,
                                                                    XLEventTracking.Disabled))
                        {
                                   
                                                        foreach (IXLWorksheet worksheet in workBook.Worksheets)
                            {
                                                 
                                                                foreach (IXLRow row in worksheet.RowsUsed().Skip(1))
                                {
                                    try
                                    {
                                        Students stud = new Students();
                                        stud.Name  = row.Cell(1).Value.ToString();
                                        stud.Photo = row.Cell(2).Value.ToString();
                                        stud.ProfileDescription = row.Cell(3).Value.ToString();
                                        _context.Students.Add(stud);

                                        if (row.Cell(4).Value.ToString().Length > 0)
                                        {
                                                                                        {
                                                AbonementTypes abtype;
                                                var            a = (from abtyp in _context.AbonementTypes
                                                                    where abtyp.Type.Contains(row.Cell(4).Value.ToString())
                                                                    select abtyp).ToList();

                                                if (a.Count > 0)
                                                {
                                                                                                    {
                                                        abtype = a[0];
                                                    }
                                                }

                                                else
                                                {
                                                    abtype      = new AbonementTypes();
                                                    abtype.Type = row.Cell(4).Value.ToString();
                                                                                                        _context.AbonementTypes.Add(abtype);
                                                }

                                                Abonements ab = new Abonements();

                                                ab.Type   = abtype;
                                                ab.TypeId = abtype.Id;
                                                _context.Abonements.Add(ab);

                                                string   dateString     = row.Cell(5).Value.ToString();
                                                DateTime dateFromString =
                                                    DateTime.Parse(dateString, System.Globalization.CultureInfo.InvariantCulture);

                                                StudentAbonements st = new StudentAbonements();

                                                st.Stud           = stud;
                                                st.Abon           = ab;
                                                st.AbonId         = ab.Id;
                                                st.StudId         = stud.Id;
                                                st.ActivationDate = dateFromString;
                                                _context.StudentAbonements.Add(st);
                                            }
                                        }
                                    }

                                    catch (Exception e)
                                    {
                                                                           
                                    }
                                }
                            }
                        }
                    }
                }
                await _context.SaveChangesAsync();
            }
            return(RedirectToAction(nameof(Index)));
        }