Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int id, EditClientWithImageViewModel viewModel)
        {
            var clientWithImage = await _context.Clients.AsNoTracking().FirstOrDefaultAsync(c => c.Id == id);


            if (id != viewModel.Client.Id)
            {//if the parameter "id" does not matche the vm's client id, then return Not Found()
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {//if the model state is valid, get the current user
                    var currentUser = await GetCurrentUserAsync();

                    //if the vm's image file is null and the clientWithImage's image file is not null, then set the vm's image file equal to the ClientWithImage's image file, other wise, set the image file to the vm's image file. This conditional ensures that if the client already has an image, and the user doesn't change or add another image during the edit, that the original image will still be in the database, instead of nothing.
                    if (viewModel.ImageFile == null && clientWithImage.ClientImage != null)
                    {
                        viewModel.Client.ClientImage = clientWithImage.ClientImage;
                    }
                    else
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            await viewModel.ImageFile.CopyToAsync(memoryStream);

                            viewModel.Client.ClientImage = memoryStream.ToArray();
                        }
                    }
                    ;
                    viewModel.Client.UserId = currentUser.Id;
                    _context.Update(viewModel.Client);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ClientExists(viewModel.Client.Id))
                    {
                        return(NotFound());
                    }

                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
            }
            return(View(viewModel));
        }
Ejemplo n.º 2
0
        // GET: Clients/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var client = await _context.Clients
                         .FindAsync(id);

            if (client == null)
            {
                return(NotFound());
            }
            //instantiate view model, where the vm's client is the client whose id matches the parameter "id"
            var viewModel = new EditClientWithImageViewModel()
            {
                Client = client
            };

            return(View(viewModel));
        }