public ActionResult Create([Bind(Include = "id,nombre,apellidos,direccionPostal,codigoPostal,localidad,provincia,pais,telefono1,telefono2,email,fechaNacimiento,fechaAlta,Sede,Perfiles")] voluntario model) { if (ModelState.IsValid) { //Get id of sede_name var sedes = db.sedes_delegaciones.SingleOrDefault(i => i.nombre == model.Sede); //Convert date var fecha = Convert.ToDateTime(model.fechaNacimiento); //Profile id is 1 for Voluntarios //Transform voluntario class in voluntarios class to allow insert, updating, deleting //Insert a new volunteer through voluntarios table var voluntariosNuevo = new voluntarios(0, model.nombre, model.apellidos, model.direccionPostal, model.codigoPostal, model.localidad, model.provincia, model.pais, model.telefono1, model.telefono2, model.email, fecha, model.fechaAlta, sedes.id); db.voluntarios.Add(voluntariosNuevo); db.SaveChanges(); //Insert the relationship many to many in personas_perfiles var perPerfilNew = new personas_perfiles(0, voluntariosNuevo.id, 1); db.persona_perfil.Add(perPerfilNew); db.SaveChanges(); TempData["Acierto"] = "El voluntario/a " + model.nombre + " " + model.apellidos + " ha sido añadido/a correctamente al sistema."; return(RedirectToAction("Index")); } return(View(model)); }
public ActionResult DeleteConfirmed(int id) { //Para vistas usar Single or default // Get voluntarios item voluntarios voluntario = db.voluntarios.SingleOrDefault(m => m.id == id); //Also needed to delete from personas_perfiles personas_perfiles per = db.persona_perfil.SingleOrDefault(m => m.idPersona == id && m.idPerfil == 1); db.persona_perfil.Remove(per); db.SaveChanges(); TempData["Acierto"] = "El voluntario/a " + voluntario.nombre + " " + voluntario.apellidos + " ha sido eliminada del sistema correctamente."; return(RedirectToAction("Index")); }
public ActionResult Edit([Bind(Include = "id,nombre,apellidos,direccionPostal,codigoPostal,localidad,provincia,pais,telefono1,telefono2,email,fechaNacimiento,fechaAlta,Sede,Perfiles")] voluntario voluntario) { if (ModelState.IsValid) { // Change default behaviour to allow modifiy entity by entity //Get id of sede_name var sedes = db.sedes_delegaciones.SingleOrDefault(i => i.nombre == voluntario.Sede); //Convert date var fecha = Convert.ToDateTime(voluntario.fechaNacimiento); //Transform voluntario class in voluntarios class to allow insert, updating, deleting //Insert a new volunteer through voluntarios table var voluntariosActualizado = new voluntarios(voluntario.id, voluntario.nombre, voluntario.apellidos, voluntario.direccionPostal, voluntario.codigoPostal, voluntario.localidad, voluntario.provincia, voluntario.pais, voluntario.telefono1, voluntario.telefono2, voluntario.email, fecha, voluntario.fechaAlta, sedes.id); db.Entry(voluntariosActualizado).State = EntityState.Modified; db.SaveChanges(); //It is no needed to update personas_perfiles because now, we do not allow to modify the profile TempData["Acierto"] = "El voluntario/a " + voluntario.nombre + " " + voluntario.apellidos + " ha sido editado correctamente."; return(RedirectToAction("Index")); } return(View(voluntario)); }