public async Task <IActionResult> Create([Bind("Id,LastName,Name,Surname,Sex,Birthday,CityId,Country,Region,Street,ApartmentNumber,PhoneNumber,Email,Skype,Facebook,FamilyStatusId, ChildrenId")] Search_Work.Models.ArreaDatabase.Candidate candidate, IFormFile Image)
        {
            if (ModelState.IsValid)
            {
                candidate.Id = Guid.NewGuid();

                if (Image != null)
                {
                    string     name       = Image.FileName;
                    string     path       = $"/files/{name}";
                    string     serverPath = $"{_environment.WebRootPath}{path}";
                    FileStream fs         = new FileStream(serverPath, FileMode.Create,
                                                           FileAccess.Write);
                    await Image.CopyToAsync(fs);

                    fs.Close();
                    candidate.Avatar = path;
                }

                _context.Add(candidate);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Edit), new { id = candidate.Id }));
            }
            ViewData["CityId"]         = new SelectList(_context.Cities, "Id", "Name", candidate.CityId);
            ViewData["FamilyStatusId"] = new SelectList(_context.FamilyStatuses, "Id", "Name");
            ViewData["ChildrenId"]     = new SelectList(_context.Childrens, "Id", "Name");
            return(View(candidate));
        }
        public async Task <IActionResult> Edit(Guid id, [Bind("Id,Avatar,LastName,Name,Surname,Sex,Birthday,CityId,Country,Region,Street,ApartmentNumber,PhoneNumber,Email,Skype,Facebook")] Search_Work.Models.ArreaDatabase.Candidate candModel, IFormFile Image)
        {
            var upCand = _context.Candidates.Include(c => c.City)
                         .Include(c => c.Resumes)
                         .FirstOrDefault(c => c.Id == id);
            var city = _context.Cities.Include(c => c.Candidates).FirstOrDefault(c => c.Id == candModel.CityId);

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

            if (ModelState.IsValid)
            {
                try
                {
                    if (Image != null)
                    {
                        string     name       = Image.FileName;
                        string     path       = $"/files/{name}";
                        string     serverPath = $"{_environment.WebRootPath}{path}";
                        FileStream fs         = new FileStream(serverPath, FileMode.Create,
                                                               FileAccess.Write);
                        await Image.CopyToAsync(fs);

                        fs.Close();
                        upCand.Avatar = path;
                    }

                    upCand.LastName        = candModel.LastName;
                    upCand.Name            = candModel.Name;
                    upCand.Surname         = candModel.Surname;
                    upCand.Sex             = candModel.Sex;
                    upCand.Birthday        = candModel.Birthday;
                    upCand.City            = candModel.City;
                    upCand.Country         = candModel.Country;
                    upCand.Region          = candModel.Region;
                    upCand.ApartmentNumber = candModel.ApartmentNumber;
                    upCand.PhoneNumber     = candModel.PhoneNumber;
                    upCand.Email           = candModel.Email;
                    upCand.Skype           = candModel.Skype;
                    upCand.Facebook        = candModel.Facebook;

                    _context.Candidates.Update(upCand);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CandidateExists(candModel.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Details", "Candidates", new { id = upCand.Id }));
            }
            ViewData["CityId"]         = new SelectList(_context.Cities, "Id", "Id", candModel.CityId);
            ViewData["FamilyStatusId"] = new SelectList(_context.FamilyStatuses, "Id", "Name");
            ViewData["ChildrenId"]     = new SelectList(_context.Childrens, "Id", "Name");
            return(View(candModel));
        }