public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var studentInformationVM = new StudentInformationVM();

            {
                StudentInformation studentInformation = await _context.StudentInformation.SingleOrDefaultAsync(c =>
                                                                                                               c.StudentID == id);

                if (studentInformation == null)
                {
                    return(NoContent());
                }
                studentInformationVM.StudentID         = studentInformation.StudentID;
                studentInformationVM.StudentName       = studentInformation.StudentName;
                studentInformationVM.FathersName       = studentInformation.FathersName;
                studentInformationVM.MothersName       = studentInformation.MothersName;
                studentInformationVM.DOB               = studentInformation.DOB;
                studentInformationVM.Religion          = studentInformation.Religion;
                studentInformationVM.GurdiansCellPhone = studentInformation.GurdiansCellPhone;
                studentInformationVM.StudentAddress    = studentInformation.StudentAddress;
                studentInformationVM.ImagePath         = studentInformation.ImagePath;

                studentInformationVM.ClassID = studentInformation.ClassID;
            }
            ViewData["ClassID"] = new SelectList(_context.ClassInformation, "ClassID", "ClassName", studentInformationVM.ClassID);
            return(View(studentInformationVM));
        }
        public async Task <IActionResult> DeleteConfirmed(int id, StudentInformationVM
                                                          studentInformationVM)
        {
            if (ModelState.IsValid)
            {
                var studentInformation = await _context.StudentInformation.FindAsync(studentInformationVM.StudentID);

                if (studentInformation == null)
                {
                    return(NotFound());
                }
                var trn = _mapper.Map <StudentInformationVM, StudentInformation>(studentInformationVM, studentInformation);
                _context.StudentInformation.Remove(trn);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ClassID"] = new SelectList(_context.ClassInformation, "ClassID", "ClassName", studentInformationVM.ClassID);
            return(View(studentInformationVM));
        }
        public async Task <IActionResult> Edit(StudentInformationVM studentInformationVM)
        {
            if (ModelState.IsValid)
            {
                var studentInformation = await
                                         _context.StudentInformation.FindAsync(studentInformationVM.StudentID);

                if (studentInformation == null)
                {
                    return(NotFound());
                }
                _mapper.Map <StudentInformationVM, StudentInformation>(studentInformationVM, studentInformation);
                _context.Entry(studentInformation).State = EntityState.Modified;
                await _context.SaveChangesAsync();

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

            return(View(studentInformationVM));
        }
        public async Task <IActionResult> Create([Bind("StudentID,StudentName,FathersName,MothersName,DOB,Religion,GurdiansCellPhone,StudentAddress,ImagePath,ClassID")] StudentInformationVM studentInformationVM, IFormFile file)
        {
            if (ModelState.IsValid)
            {
                _context.Add(studentInformationVM);
                await _context.SaveChangesAsync();

                // Code to upload image if not null
                if (file != null || file.Length != 0)
                {
                    // Create a File Info
                    FileInfo fi = new FileInfo(file.FileName);

                    // This code creates a unique file name to prevent duplications
                    // stored at the file location
                    var newFilename = studentInformationVM.StudentID + "_" + String.Format("{0:d}",
                                                                                           (DateTime.Now.Ticks / 10) % 100000000) + fi.Extension;
                    var webPath = hostingEnvironment.WebRootPath;
                    var path    = Path.Combine("", webPath + @"\ImageFiles\" + newFilename);

                    // IMPORTANT: The pathToSave variable will be save on the column in the database
                    var pathToSave = @"/ImageFiles/" + newFilename;

                    // This stream the physical file to the allocate wwwroot/ImageFiles folder
                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    // This save the path to the record
                    studentInformationVM.ImagePath = pathToSave;
                    _context.Update(studentInformationVM);
                    await _context.SaveChangesAsync();
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["ClassID"] = new SelectList(_context.ClassInformation, "ClassID", "ClassName", studentInformationVM.ClassID);
            return(View(studentInformationVM));
        }