//[Route, HttpPut]

        public IHttpActionResult PutContacto(int id, Contacto contacto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != contacto.IdContacto)
            {
                return(BadRequest());
            }

            var telUpd = contacto.Telefonos.Where(t => t.IdTelefono > 0).ToList();
            var telNew = contacto.Telefonos.Where(t => t.IdTelefono == 0).ToList();

            try
            {
                db.Entry(contacto).State = EntityState.Modified;
                telUpd.ForEach(t =>
                {
                    db.Entry(t).State = EntityState.Modified;
                });
                telNew.ForEach(t =>
                {
                    t.Contacto = contacto;
                    db.Telefonos.Add(t);
                });
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ContactoExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            catch (Exception err)
            {
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Esempio n. 2
0
        public async Task <IActionResult> PutContactos(int id, Contacto item)
        {
            if (id != item.Id)
            {
                return(BadRequest());
            }
            _context.Entry(item).State = EntityState.Modified;
            await _context.SaveChangesAsync();

            return(NoContent());
        }