Пример #1
0
        // GET: ClientPages/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var clientPage = await _context.ClientPages.FindAsync(id);

            var clientUsers = await _context.ApplicationUsers
                              .Where(u => u.UserTypeId == 3)
                              .ToListAsync();

            var clientUserOptions = clientUsers.Select(u => new SelectListItem(u.FirstName + " " + u.LastName, u.Id)).ToList();

            var viewModel = new ClientPageCreateEditViewModel()
            {
                ClientPage        = clientPage,
                ClientUsers       = clientUsers,
                ClientUserOptions = clientUserOptions
            };


            return(View(viewModel));
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, ClientPageCreateEditViewModel viewModel)
        {
            var clientPage = viewModel.ClientPage;

            if (id != clientPage.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var currentFileName = viewModel.ClientPage.ImgPath;
                    if (viewModel.Img != null && viewModel.Img.FileName != currentFileName)
                    {
                        if (currentFileName != null)
                        {
                            var images       = Directory.GetFiles("wwwroot/images");
                            var fileToDelete = images.First(i => i.Contains(currentFileName));
                            System.IO.File.Delete(fileToDelete);
                        }
                        var uniqueFileName = GetUniqueFileName(viewModel.Img.FileName);
                        var imageDirectory = Path.Combine(_webHostEnvironment.WebRootPath, "images");
                        var filePath       = Path.Combine(imageDirectory, uniqueFileName);
                        using (var myFile = new FileStream(filePath, FileMode.Create))
                        {
                            viewModel.Img.CopyTo(myFile);
                        }
                        viewModel.ClientPage.ImgPath = uniqueFileName;
                    }
                    if (viewModel.ClientUserId != null)
                    {
                        var clientPageUser = new ClientPageUser()
                        {
                            UserId       = viewModel.ClientUserId,
                            ClientPageId = viewModel.ClientPage.Id
                        };
                        _context.Add(clientPageUser);
                        await _context.SaveChangesAsync();
                    }
                    _context.Update(clientPage);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ClientPageExists(clientPage.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(clientPage));
        }
Пример #3
0
        public async Task <IActionResult> Create(ClientPageCreateEditViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                if (viewModel.Img != null)
                {
                    var uniqueFileName = GetUniqueFileName(viewModel.Img.FileName);
                    var imageDirectory = Path.Combine(_webHostEnvironment.WebRootPath, "images");
                    var filePath       = Path.Combine(imageDirectory, uniqueFileName);
                    using (var myFile = new FileStream(filePath, FileMode.Create))
                    {
                        viewModel.Img.CopyTo(myFile);
                    }
                    viewModel.ClientPage.ImgPath = uniqueFileName;
                }
                _context.Add(viewModel.ClientPage);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewModel));
        }
Пример #4
0
        // GET: ClientPages/Create
        public IActionResult Create()
        {
            var viewModel = new ClientPageCreateEditViewModel();

            return(View(viewModel));
        }