예제 #1
0
        public async Task <IActionResult> Add(PhoneBookAddModel model) // async used to save image to stream
        {
            if (ModelState.IsValid)
            {
                // Control of ImageSize
                if (model.Image == null || model.Image.Length == 0)
                {
                    model.ImageName = "defaultpp.jpg";
                }

                else
                {
                    // 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
                    model.ImageName = model.Image.FileName;
                }

                Person person = new Person();
                person.Name         = model.Name;
                person.Surname      = model.Surname;
                person.PhoneNumber1 = model.PhoneNumber1;
                person.PhoneNumber2 = model.PhoneNumber2;
                person.Email        = model.Email;
                person.ImageName    = model.ImageName;

                _phoneBookDb.Add(person);

                TempData["message"] = "Your contact added to database succesfully.";
                TempData["status"]  = "alert alert-success";
                return(RedirectToAction("Index", "Home"));
            }

            TempData["message"] = "Your contact did not add to database !";
            TempData["status"]  = "alert alert-danger";
            return(RedirectToAction("Index", "Home"));
        }