Esempio n. 1
0
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var text = await _context.Texts.FindAsync(id);

            if (text == null)
            {
                return(NotFound());
            }
            var textVm = new EditTextVewModel()
            {
                Id          = text.Id,
                Title       = text.Title,
                Paragraph1  = text.Paragraph1,
                Paragraph2  = text.Paragraph2,
                Paragraph3  = text.Paragraph3,
                Paragraph4  = text.Paragraph4,
                Paragraph5  = text.Paragraph5,
                Paragraph6  = text.Paragraph6,
                Paragraph7  = text.Paragraph7,
                Paragraph8  = text.Paragraph8,
                Paragraph9  = text.Paragraph9,
                Paragraph10 = text.Paragraph10,
                TextType    = text.TextType
            };

            return(View(textVm));
        }
Esempio n. 2
0
        public async Task <IActionResult> Edit(EditTextVewModel textVm)
        {
            if (ModelState.IsValid)
            {
                var text = await _context.Texts.FindAsync(textVm.Id);

                if (textVm.Photo != null)
                {
                    var uniqueFileName = GetUniqueFileName(textVm.Photo.FileName);
                    var uploads        = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
                    var filePath       = Path.Combine(uploads, uniqueFileName);
                    textVm.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                    text.Photo = uniqueFileName;
                }
                text.Title       = textVm.Title;
                text.Paragraph1  = textVm.Paragraph1;
                text.Paragraph2  = textVm.Paragraph2;
                text.Paragraph3  = textVm.Paragraph3;
                text.Paragraph4  = textVm.Paragraph4;
                text.Paragraph5  = textVm.Paragraph5;
                text.Paragraph6  = textVm.Paragraph6;
                text.Paragraph7  = textVm.Paragraph7;
                text.Paragraph8  = textVm.Paragraph8;
                text.Paragraph9  = textVm.Paragraph9;
                text.Paragraph10 = textVm.Paragraph10;
                text.TextType    = textVm.TextType;
                text.DateEdited  = DateTime.Now.ToString("dd.MM.yyyy");
                try
                {
                    _context.Update(text);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TextExists(text.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(IndexAdmin)));
            }
            return(View(textVm));
        }