Exemple #1
0
        // GET: Vacations/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Vacantion vacantion = await _context.Vacantions.Include(v => v.FromUser).FirstAsync(u => u.Id == id);

            if (vacantion == null)
            {
                return(NotFound());
            }
            VacationsEditViewModel model = new VacationsEditViewModel
            {
                Id               = vacantion.Id,
                FromDate         = vacantion.FromDate,
                ToDate           = vacantion.ToDate,
                Type             = vacantion.Type,
                HalfDayVacantion = vacantion.HalfDayVacantion,
            };

            if (model.Type == "sick")
            {
                model.CurrentImage = vacantion.ImageUpload;

                /*var stream = new MemoryStream(model.CurrentImage);
                 * model.ImageUpload = new FormFile(stream, 0, model.CurrentImage.Length, "file", "sickSheet");*/
            }

            return(View(model));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(VacationsEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                Vacantion vacation = await _context.Vacantions.Include(v => v.FromUser).FirstAsync(u => u.Id == model.Id);

                vacation.Id               = model.Id;
                vacation.FromDate         = model.FromDate;
                vacation.ToDate           = model.ToDate;
                vacation.Type             = model.Type;
                vacation.HalfDayVacantion = model.HalfDayVacantion;
                vacation.DateOfCreation   = DateTime.Now;
                vacation.Editted          = true;

                if (vacation.Type == "sick")
                {
                    vacation.HalfDayVacantion = false;
                    vacation.ImageUpload      = model.CurrentImage;

                    if (model.CurrentImage == null && model.ImageUpload == null)
                    {
                        ModelState.AddModelError("ImageUpload", "Must upload image of sheet or record.");
                        return(await Edit(model.Id));
                    }
                    else if (model.ImageUpload != null)
                    {
                        if (model.ImageUpload.Length > 0)
                        //Convert Image to byte and save to database
                        {
                            byte[] p1 = null;
                            using (var fs1 = model.ImageUpload.OpenReadStream())
                                using (var ms1 = new MemoryStream())
                                {
                                    fs1.CopyTo(ms1);
                                    p1 = ms1.ToArray();
                                }
                            vacation.ImageUpload = p1;
                        }
                    }
                }
                else
                {
                    vacation.ImageUpload = null;
                }


                _context.Update(vacation);
                _context.SaveChanges();

                return(RedirectToAction("Details", "Vacations", new { id = vacation.Id }));
            }

            return(View(model));
        }