Exemple #1
0
        public Contact CreateContact(int groupId, Contact contactToCreate)
        {
            // Associate group with contact
            contactToCreate.Group = GetGroup(groupId);

            // Save new contact
            _entities.AddToContactSet(contactToCreate);
            _entities.SaveChanges();
            return(contactToCreate);
        }
        public ActionResult Create([Bind(Exclude = "Id")]Contact newContact)
        {
            // El componente Model Binder automaticamente recupera los valores del formulario
            // y crea un objeto Contact, basandose en el nombre de las propiedades

            if (!ModelState.IsValid)
            {
                return View();
            }

            try
            {
                using (var ctx = new ContactManagerDBEntities())
                {
                    // Añadimos el nuevo objeto al contexto actual y enviamos los cambios a la bd
                    ctx.AddToContacts(newContact);
                    ctx.SaveChanges();
                }

                // Pattern PRG (Post/Redirect/Get): Si todo sale bien redirigimos al usuario a la lista
                // inicial, en lugar de  mostrar un mensaje en la vista actual, evitando posibles duplicidades
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Delete(int id, Contact delContact)
        {
            try
            {
                using (var ctx = new ContactManagerDBEntities())
                {
                    var contact = ctx.Contacts.SingleOrDefault(c => c.Id == id);
                    ctx.DeleteObject(contact);
                    ctx.SaveChanges();

                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }
        public ActionResult Edit(Contact editedContact)
        {
            // Si hay errores en el modelo volvemos a presentar el formulario
            if (!ModelState.IsValid)
            {
                return View();
            }
            try
            {

                using (var ctx = new ContactManagerDBEntities())
                {
                    // Recuperamos el Contacto Original, para que EF pueda gestionar
                    // automaticamente la modificacion
                    var originalContact = ctx.Contacts.SingleOrDefault(c => c.Id == editedContact.Id);

                    // Aplicamos los cambios
                    ctx.Contacts.ApplyCurrentValues(editedContact);
                    ctx.SaveChanges();

                    // Post/Redirect/Get
                    return RedirectToAction("Index");
                }
            }
            catch
            {
                return View();
            }
        }
 public Contact CreateContact(Contact contactToCreate)
 {
     _entities.Entry(contactToCreate).State = EntityState.Added;
     _entities.SaveChanges();
     return(contactToCreate);
 }
Exemple #6
0
 public Contact CreateContact(Contact contactToCreate)
 {
     _entities.AddToContactSet(contactToCreate);
     _entities.SaveChanges();
     return(contactToCreate);
 }