public ActionResult Edit(Cantante cantante) { //DE ESTA MANERA TAMBIEN FUNCIONA //if (ModelState.IsValid) //{ // var elCantante = CantanteContex.Cantantes.Find(c => c.Id == cantante.Id); // elCantante.Nombre = cantante.Nombre; // elCantante.Voz = cantante.Voz; // elCantante.Id = cantante.Id; // return RedirectToAction("Index"); //} //else //{ // return RedirectToAction("Error"); //} int index = CantanteContex.Cantantes.FindIndex(c => c.Id == cantante.Id); if ((CantanteContex.Cantantes[index] != null) && (ModelState.IsValid)) { CantanteContex.Cantantes[index] = cantante; return RedirectToAction("Index"); } else { return RedirectToAction("Error"); } }
public ActionResult Create(Cantante cantante) { if (ModelState.IsValid)//Valida el formulario y si todo va bien entonces sigue { //AQUI ES DONDE DEBE ASIGNARSE Y CREARSE EL ID PARA ALMACENARLO LUEGO //cantante.Id = CantanteContex.Cantantes.Count+1; CantanteContex.Cantantes.Add(cantante); //new Cantante() //{ // Id = CantanteContex.Cantantes.Count, // Nombre = Nombre, // Voz = Voz //}); return RedirectToAction("Index"); } else { return View("Create", cantante); } }