Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Type")] AbonementTypes abonementTypes)
        {
            if (id != abonementTypes.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(abonementTypes);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AbonementTypesExists(abonementTypes.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(abonementTypes));
        }
Пример #2
0
        public async Task <IActionResult> Create([Bind("Id,Type")] AbonementTypes abonementTypes)
        {
            if (ModelState.IsValid)
            {
                _context.Add(abonementTypes);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(abonementTypes));
        }
Пример #3
0
        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)));
        }