View data is not updated

1

I have a list of clients that I have to update, but when changing the values, when I click on update, the original data reappears.

Model Cliente :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ejercicio03.Models
{
    public class Cliente
    {
        public int id { get; set; }
        public string nombre { get; set; }
        public string apellido { get; set; }
    }
}

Class ClienteServicio :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ejercicio03.Models
{
    public class ClienteServicio
    {
        // INSTANCIAMOS 
        public static List<Cliente> lista = new List<Cliente>();

        // CONSTRUCTOR SIN PARAMETROS
        public ClienteServicio()
        {
            // INICIALIZAMOS
            if (lista.Count == 0)
            {
                this.altaCliente(1, "Julian", "Butron");
                this.altaCliente(2, "Pedro", "Picapiedra");
                this.altaCliente(3, "Pablo", "Marmol");
            }
        }

        // CONSTRUCTOR CON PARAMETROS
        public ClienteServicio(int id, string nombre, string apellido)
        {
            this.altaCliente(id, nombre, apellido);
        }

        // GETTER: MOSTRAR LISTA
        public List<Cliente> mostrarCliente()
        {
            return lista;
        }

        // SETTER: ESTABLECER CLIENTE
        public void altaCliente(int id, string nombre, string apellido)
        {
           lista.Add(new Cliente() { id = id, nombre = nombre, apellido = apellido });
        }
    }

View Todos :

@Model Ejercicio03.Models.Cliente 

@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Todos</title>
</head>
<body style="font-family:Arial,Helvetica">
    <a href="Bienvenido"><input type="button" value="Volver" style="margin-left:635px;margin-top:18px;"/></a>
    <div style="width:600px;margin:auto;margin-top:-45px;background-color:#DD0611;color:white;">      
        <h2 style="padding-top:20px;text-align:center;">CLIENTES</h2>
        <a href="AltaCliente"><input type="button" value="Nuevo Cliente" style="margin: 0 0 6px 6px;padding:4px;background-color:#1DAE46;color:white;"/></a>
    </div>
    <table align="center" width="600" border="0" cellpadding="0" cellspacing="0" bgcolor="#ECEBEB" padding="10px">
        <tr>
            <th style="padding:3px;border-bottom: 1px solid #fff">Id</th>
            <th style="padding:3px;border-bottom: 1px solid #fff">Nombre</th>
            <th style="padding:3px;border-bottom: 1px solid #fff">Apellido</th>
            <th colspan='2' style="padding:3px;border-bottom: 1px solid #fff">Operaciones</th>
        </tr>
        @foreach (Ejercicio03.Models.Cliente c in Model)
        {
            <tr>
                <td style="text-align:center;padding:3px;border-bottom: 1px solid #fff;">@c.id</td>
                <td style="text-align:center;padding:3px;border-bottom: 1px solid #fff;">@c.nombre</td>
                 <td style="text-align:center;padding:3px;border-bottom: 1px solid #fff;">@c.apellido</td>
                <td style="text-align:center;padding:3px;border-bottom: 1px solid #fff;"><a href="[email protected]"><input type="button" value="Mostrar" /></a></td>
                <td style="text-align:center;padding:3px;border-bottom: 1px solid #fff;"><a href="[email protected]"><input type="button" value="Editar" /></a></td>
            </tr>
        }
    </table>
</body>
</html>

View EditarCliente :

@Model Ejercicio03.Models.Cliente
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>EditarCliente</title>
</head>
<body style="font-family:Arial,Helvetica">
    <div style="width:600px;margin:auto;margin-top:40px;padding:5px;background-color:#DD0611;color:white;box-sizing: border-box;">
        <h2 style="text-align:center;">Editar Cliente</h2>
    </div>
    <form action="EditarCliente" method="post" style="margin:auto;">

        <table align="center" width="600" border="0" cellpadding="0" cellspacing="0" bgcolor="#ECEBEB" ;margin="0" ;padding="0" ;>
            @foreach (Ejercicio03.Models.Cliente c in Model)
            {
                if (c.id == Convert.ToInt16(Request["id"]))
                {
            <tr>
                <td style="text-align:right;padding:3px;">Nombre</td>
                <td style="text-align:center;padding:3px;"><input type="text" name="nombre" value="@c.nombre"/></td>
            </tr>
            <tr>
                <td style="text-align:right;padding:3px;">Apellido</td>
                <td style="text-align:center;padding:3px;"><input type="text" name="apellido" value="@c.apellido"/></td>
            </tr>
            <tr>
                <td><input type="hidden" name="id" value="@c.id" /></td>
                <td colspan='2' style="text-align:center;padding:20px;"><input type="submit" value="ACTUALIZAR" style="padding:6px;background-color:#1DAE46;color:white;" /></td>
            </tr>
                }
            }
        </table>
    </form>
</body>
</html>

Controller Cliente :

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

namespace Ejercicio03.Controllers
{
    public class ClienteController : Controller
    {
        // GET: Cliente

        // BIENVENIDO
        public ActionResult Bienvenido()
        {
            return View();
        }

        // LISTADO DE CLIENTES
        public ActionResult Todos()
        {
            ClienteServicio cs = new ClienteServicio();
            var listado = cs.mostrarCliente();

            return View(listado);
        }

        // ALTA CLIENTE
        public ActionResult AltaCliente()
        {
            return View();
        }

        [HttpPost]  // RECIBE LOS DATOS DE LA VISTA "AltaCliente".
        public ActionResult AltaCliente(Cliente c)
        {

            ClienteServicio cs = new ClienteServicio(c.id, c.nombre, c.apellido);
            return View("Bienvenido"); // Retorna a la vista "Bienvenido"
        }

        // MOSTRAR DETALLE CLIENTE
        public ActionResult DetalleCliente()
        {
            ClienteServicio cs = new ClienteServicio();
            var detalle = cs.mostrarCliente();

            return View(detalle);
        }

        // EDITAR CLIENTE
        public ActionResult EditarCliente()
        {
            ClienteServicio cs = new ClienteServicio();
            var cli = cs.mostrarCliente();

            return View(cli);
        }

      /*  [HttpPost]  // RECIBE LOS DATOS DE LA VISTA "EditarCliente".
        public ActionResult EditarClienteProc(Cliente c)
        {
            ClienteServicio cs = new ClienteServicio(c.id, c.nombre, c.apellido);
            return View("Bienvenido");
        }
        */
    }
}

What is happening?

    
asked by Andrés Oporto 08.05.2017 в 19:40
source

1 answer

0

I see several problems.

On the one hand you have not implemented the action that receives the data from the EditClient form (or more exactly you have it commented). The implementation could be something like this:

    [HttpPost]  // RECIBE LOS DATOS DE LA VISTA "EditarCliente".
    public ActionResult EditarCliente(Cliente c)
    {
        ClienteServicio cs = new ClienteServicio(c.id, c.nombre, c.apellido);
        return View("Todos", cs.mostrarCliente());
    }

In this way, once the client has been edited, it is redirected to the full list of clients.

On the other hand, when you call the ServiceCustomer constructor with parameters, you always sign up for a new client. You should check if a client already exists with the id (it's an edition) or if it does not exist (it's a new customer's registration). Something like this:

    // CONSTRUCTOR CON PARAMETROS
    public ClienteServicio(int id, string nombre, string apellido)
    {
        var cliente = lista.FirstOrDefault(c => c.id == id);
        if (cliente == null)
        {
            // El cliente no exite
            altaCliente(id, nombre, apellido);
        }
        else
        {
            // El cliente sí existe editamos sus datos
            cliente.nombre = nombre;
            cliente.apellido = apellido;
        }
    }
    
answered by 08.05.2017 / 20:33
source