Beispiel #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id, Name, AddedOn, Tag, AddedBy, FileSize")] Document document, IFormFile file)
        {
            if (id != document.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (file == null)
                    {
                        document.Name       = document.Name;
                        document.Tag        = document.Tag;
                        document.AddedBy    = document.AddedBy;
                        document.LastEdited = DateTime.Now;

                        _context.Update(document);
                        await _context.SaveChangesAsync();
                    }
                    else if (file.Length > 0)
                    {
                        var folder         = Path.Combine(webHostEnvironment.WebRootPath, "files");
                        var uniqueFileName = Guid.NewGuid().ToString() + "_" + file.FileName;
                        var filePath       = Path.Combine(folder, uniqueFileName);

                        using var stream = new FileStream(filePath, FileMode.Create);
                        await file.CopyToAsync(stream);

                        document.Name       = file.FileName;
                        document.FileSize   = file.Length;
                        document.Tag        = document.Tag;
                        document.AddedOn    = document.AddedOn;
                        document.LastEdited = DateTime.Now;
                        document.AddedBy    = document.AddedBy;
                        document.FilePath   = uniqueFileName;

                        _context.Update(document);
                        await _context.SaveChangesAsync();
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!DocumentExists(document.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(document));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,AddedOn,AddedBy")] Tag tag)
        {
            if (id != tag.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(tag);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!TagExists(tag.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(tag));
        }