public async Task <IActionResult> Put(int id, [FromBody] Contact contact)
        {
            using (var db = new ContactsDB())
            {
                var c = db.Contacts.Find(contact.ContactID);

                if (c == null)
                {
                    return(NotFound());
                }

                db.Entry(c).CurrentValues.SetValues(contact);
                db.SaveChanges();
                return(Ok());

                //new BodyModelBinder().BindModelAsync();
                if (await base.TryUpdateModelAsync(c))
                {
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
        }
Exemple #2
0
        public ActionResult Edit(ContactsViewModel editContact, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                Contact existingContact = _db.Contacts.Find(editContact.Id);

                existingContact.Id          = editContact.Id;
                existingContact.FirstName   = editContact.FirstName;
                existingContact.LastName    = editContact.LastName;
                existingContact.City        = editContact.City;
                existingContact.Description = editContact.Description;
                existingContact.HomePhone   = editContact.HomePhone;
                existingContact.WorkPhone   = editContact.WorkPhone;
                existingContact.MobilePhone = editContact.MobilePhone;

                if (image != null)
                {
                    existingContact.Image = new byte[image.ContentLength];
                    image.InputStream.Read(existingContact.Image, 0, image.ContentLength);
                }

                _db.Entry(existingContact).State = EntityState.Modified;
                _db.SaveChanges();
            }
            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Edit([Bind(Include = "ContactId,Name,EMail,Phone1,Phone2,Address1,Address2,City,State,Zip")] Contact contact)
        {
            if (ModelState.IsValid)
            {
                db.Entry(contact).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(contact));
        }