MVC: Loading incorrect list

2

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
                }
            };
        }

    }
}
    
asked by vickry 10.09.2017 в 15:13
source

2 answers

2

It's because you're using 2 different lists. In the action Edit that is executed with GET , you load the list that returns the method TodosLosCliente() :

 // GET: Clientes/Edit/5
public ActionResult Edit(int id)
{
    List<Clientes> empList = TodosLosClientes();// aqui utilizas la lista que retorna este metodo
    var Clientes = empList.Single(m => m.ID == id);
    return View(Clientes);
}

While when you edit the client, you load the list of the object empList declared static on the controller:

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
    try
    {

        // aqui estas cargando la lista del empList statico, no del metodo TodosLosClients()
        var Clientes = empList.Single(m => m.ID == id);
        if (TryValidateModel(Clientes))
            return RedirectToAction("Index");

        return View(Clientes);

    }
    catch
    {
        return View();
    }
}

Both the method TodosLosClientes() and the list empList have different records which can explain that it does not show you the same results.

    
answered by 10.09.2017 в 18:06
0

you must change to:

using MiPrimerAplicacion.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MiPrimerAplicacion.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: ClientesMVC
        public ActionResult Index()
        {
            var Clientes = from e in empList
                           orderby e.ID
                           select e;
            return View(Clientes);
        }

        // GET: ClientesMVC/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: ClientesMVC/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: ClientesMVC/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: ClientesMVC/Edit/5
        public ActionResult Edit(int id)
        {
            //List<Clientes> empList = TodosLosClientes();
            var Clientes = empList.Single(m => m.ID == id);
            return View(Clientes);
        }

        // POST: ClientesMVC/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var Clientes = empList.Single(m => m.ID == id);
                if (TryUpdateModel(Clientes))
                {
                    return RedirectToAction("Index");                    
                }
                return View(Clientes);
            }
            catch
            {
                return View();
            }

        }

        // GET: ClientesMVC/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: ClientesMVC/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }




    }
    }

in the example uses two lists, one is deleted and used in the whole example that

    
answered by 22.02.2018 в 03:46