public async Task <IActionResult> Create(CreateClientWithImageViewModel vm) { ModelState.Remove("client.UserId"); if (ModelState.IsValid) { //If the model is valid, then the client will be saved to the current user's list of clients, and the client will have a userId of the current user. The new client will be added to the list of clients. var currentUser = await GetCurrentUserAsync(); if (vm.ImageFile != null) {//Method where if the image file contains an image, then that image will be displayed with the view using (var memoryStream = new MemoryStream()) { await vm.ImageFile.CopyToAsync(memoryStream); vm.Client.ClientImage = memoryStream.ToArray(); } } ; //Set the new client's userId to the current user's id and add it the the current user's list of clients vm.Client.UserId = currentUser.Id; _context.Add(vm.Client); await _context.SaveChangesAsync(); //After saving the new client, the user will be directed back to the Clients Index view return(RedirectToAction(nameof(Index))); } return(View(vm)); }
// GET: Clients/Create public IActionResult Create() {//instantiate view model, that allows the client's image to be displayed on the view CreateClientWithImageViewModel vm = new CreateClientWithImageViewModel(); return(View(vm)); }