コード例 #1
0
        public IActionResult Update(int id)
        {
            var updatedPerson = _phoneBookDb.GetById(id);

            var model = new PhoneBookUpdateModel
            {
                Id           = updatedPerson.Id,
                Name         = updatedPerson.Name,
                Surname      = updatedPerson.Surname,
                PhoneNumber1 = updatedPerson.PhoneNumber1,
                PhoneNumber2 = updatedPerson.PhoneNumber2,
                Email        = updatedPerson.Email,
                ImageName    = updatedPerson.ImageName
            };

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> Update(PhoneBookUpdateModel model)
        {
            if (ModelState.IsValid)
            {
                var updatedPerson = _phoneBookDb.GetById(model.Id); // Person is exist or not

                if (updatedPerson != null)
                {
                    updatedPerson.Id           = model.Id;
                    updatedPerson.Name         = model.Name;
                    updatedPerson.Surname      = model.Surname;
                    updatedPerson.PhoneNumber1 = model.PhoneNumber1;
                    updatedPerson.PhoneNumber2 = model.PhoneNumber2;
                    updatedPerson.Email        = model.Email;

                    // If client chooses new image
                    if (model.Image != null)
                    {
                        // Save image to path
                        var path = Path.Combine(_hostingEnvironment.ContentRootPath, "wwwroot\\images", model.Image.FileName);

                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await model.Image.CopyToAsync(stream);
                        }

                        //This variable use for person.ImageName
                        updatedPerson.ImageName = model.Image.FileName;
                    }

                    _phoneBookDb.Update(updatedPerson);

                    TempData["message"] = "Person updated succesfully.";
                    TempData["status"]  = "alert alert-warning";
                    return(RedirectToAction("Index", "Home"));
                }
            }

            TempData["message"] = "Person did not update !";
            TempData["status"]  = "alert alert-danger";
            return(RedirectToAction("Index", "Home"));
        }