Greetings, help please, when I EDIT some value of the properties of the collection this is not reflected in the returned views, for example, when I try to EDIT the age allows me to do it, but when I save I do not see the change reflected, because I'm loading a different list than the one I'm editing.
namespace ModelClass.Controllers
{
public class ClientesController : Controller
{
public static List<Clientes> empList = new List<Clientes>
{
new Clientes
{
ID = 1,
nombre = "Vickry",
FechaAlta = DateTime.Parse(DateTime.Today.ToString()),
edad = 30
},
new Clientes
{
ID = 2,
nombre = "Jeneury",
FechaAlta = DateTime.Parse(DateTime.Today.ToString()),
edad = 26
},
new Clientes
{
ID = 3,
nombre = "Sebas",
FechaAlta = DateTime.Parse(DateTime.Today.ToString()),
edad = 2
}
};
// GET: Clientes
public ActionResult Index()
{
var Clientes = from e in empList
orderby e.ID
select e;
return View(Clientes);
}
// GET: Clientes/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Clientes/Create
public ActionResult Create()
{
return View();
}
// POST: Clientes/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Clientes/Edit/5
public ActionResult Edit(int id)
{
List<Clientes> empList = TodosLosClientes();
var Clientes = empList.Single(m => m.ID == id);
return View(Clientes);
}
// POST: Clientes/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
var Clientes = empList.Single(m => m.ID == id);
if (TryValidateModel(Clientes))
return RedirectToAction("Index");
return View(Clientes);
}
catch
{
return View();
}
}
// GET: Clientes/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: Clientes/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View();
}
}
[NonAction]
public List<Clientes> TodosLosClientes()
{
return new List<Clientes>
{
//para agragar a la nueva lista un cliente (objeto)
//necesariamente hay que instanciarlo tambien dentro de la lista
new Clientes
{
ID = 1,
nombre = "Miguel",
FechaAlta = DateTime.Parse(DateTime.Today.ToString()),
edad = 30
},
new Clientes
{
ID = 2,
nombre = "Jene",
FechaAlta = DateTime.Parse(DateTime.Today.ToString()),
edad = 26
},
new Clientes
{
ID = 3,
nombre = "Sebas",
FechaAlta = DateTime.Parse(DateTime.Today.ToString()),
edad = 2
}
};
}
}
}