Example #1
0
        public async Task <IActionResult> Create(ContactCreateViewModel model)
        {
            if (!TryValidateModel(model))
            {
                return(View());
            }

            Contact contact = new Contact()
            {
                Voornaam      = model.Voornaam,
                Naam          = model.Naam,
                Geslacht      = model.Geslacht,
                Geboortedatum = model.Geboortedatum,
                Adres         = model.Adres,
                Beschrijving  = model.Beschrijving,
                Email         = model.Email,
                TelefoonNr    = model.TelefoonNr,
            };

            using (var memoryStream = new MemoryStream())
            {
                await model.Avatar.CopyToAsync(memoryStream);

                contact.Avatar = memoryStream.ToArray();
            }

            _contactDabase.Insert(contact);

            return(RedirectToAction("Detail", new { id = contact.Id }));
        }
Example #2
0
        public IActionResult Create(ContactCreateViewModel contact)
        {
            if (!TryValidateModel(contact))
            {
                return(View(contact));
            }

            var contactToDb = new Contact()
            {
                FirstName   = contact.FirstName,
                LastName    = contact.LastName,
                PhoneNumber = contact.PhoneNumber,
                Address     = contact.Address,
                Email       = contact.Email,
                Description = contact.Description,
                BirthDate   = contact.BirthDate,
                ContactType = contact.ContactType,
            };

            if (contact.Avatar != null)
            {
                var uniqueFileName = Guid.NewGuid().ToString() + Path.GetExtension(contact.Avatar.FileName);
                var path           = Path.Combine(_hostEnvironment.WebRootPath, "photos", uniqueFileName);
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    contact.Avatar.CopyTo(stream);
                }
                contactToDb.PhotoUrl = "/photos/" + uniqueFileName;
            }

            _contactDatabase.Insert(contactToDb);

            return(RedirectToAction("Index"));
        }
Example #3
0
        public IActionResult Create(ContactCreateViewModel contact)
        {
            byte[] file;

            if (contact.Avatar != null)
            {
                file = GetBytesFromFile(contact.Avatar);
            }
            else
            {
                file = new byte[] { };
            }

            Contact newContact = new Contact()
            {
                Naam          = contact.Naam,
                Voornaam      = contact.Voornaam,
                Geslacht      = contact.Geslacht,
                GeboorteDatum = contact.GeboorteDatum,
                Email         = contact.Email,
                TelefoonNr    = contact.TelefoonNr,
                Adres         = contact.Adres,
                Beschrijving  = contact.Beschrijving,
                Avatar        = file,
                Category      = contact.Category
            };

            _contactDatabase.Insert(newContact);

            return(RedirectToAction("Contactenoverzicht"));
        }
Example #4
0
        public IActionResult Create([FromForm] ContactCreateViewModel contact)
        {
            if (TryValidateModel(contact))
            {
                _contactDatabase.Insert(new Contact
                {
                    Name       = contact.Name,
                    Surname    = contact.Surname,
                    Email      = contact.Email,
                    BirthDate  = contact.BirthDate,
                    TelNr      = contact.TelNr,
                    Address    = contact.Address,
                    Annotation = contact.Annotation
                });

                return(RedirectToAction(nameof(Index)));
            }
            return(View());
        }