Unable to read property when merging two entities into one

2

I am placing two models in one through a class that unites them, with the objective of assigning them value that I get from the DB but it generates an exception in the first line that reads the read() -> Test.Models.Modelo.Pagos.get returned null, I do not know that does not obtain value, the classes are in separate files

Controller :

public ActionResult Index(){
    List<Modelo> obtener = GetPersonas();
    return View(obtener);
}

public List<Modelo> GetPersonas(){
    List<Modelo> lstPagos = new List<Modelo>();
    SqlConnection con = new SqlConnection("Server=PC;Database=Test;Trusted_Connection=yes;");
    SqlCommand cmd = new SqlCommand("SELECT p.Id_pago,c.Id_cliente,c.Nombre,p.Monto,c.Comision,p.Autorizacion,p.Comentario,p.Fecha FROM Pagos p inner join Cliente c on p.Id_cliente=c.Id_cliente where p.Autorizacion=0", con);
    cmd.CommandType = CommandType.Text;

    con.Open();
    SqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read()){
        Modelo persona = new Modelo();
        persona.Pagos.IdPago = Convert.ToInt16(dr["Id_pago"]);
        persona.Pagos.IdCliente = Convert.ToInt16(dr["Id_cliente"]);
        persona.Cliente.Nombre = dr["Nombre"].ToString();
        persona.Pagos.Monto = Convert.ToDecimal(dr["Monto"]);
        persona.Cliente.Comision = Convert.ToInt32(dr["Comision"]);
        persona.Pagos.Autorizacion = Convert.ToBoolean(dr["Autorizacion"]);
        persona.Pagos.Fecha= Convert.ToDateTime( dr["Fecha"]);

        lstPagos.Add(persona);
    }

    con.Close();
    return lstPagos;
}

class Client :

public class Cliente{
    public int IdCliente { get;  set; }
    public string Nombre { get;  set; }
    public int Comision { get;  set; }
    public int Estatus { get;  set; }
}

payment class :

public class Pagos{
    public int IdPago { get; set; }
    public int IdCliente { get; set; }
    public Decimal Monto { get; set; }
    public bool Autorizacion { get; set; }
    public string Comentario { get; set; }
    public DateTime Fecha { get; set; }
    public string NombreCliente { get; set; }

}

Model class :

public class Modelo{
    public Pagos Pagos { get; private set; }
    public Cliente Cliente { get; private set; }
}
    
asked by Ivxn 21.07.2018 в 00:37
source

1 answer

5

I commented to you, to manipulate the properties of one object inside another, this one has to build (or instantiate) first.

Modelo persona = new Modelo();
//Creamos Pagos
persona.Pagos = new Pagos();
persona.Pagos.IdPago = Convert.ToInt16(dr["Id_pago"]);
persona.Pagos.IdCliente = Convert.ToInt16(dr["Id_cliente"]);
//Creamos Cliente
persona.Cliente = new Cliente();
persona.Cliente.Nombre = dr["Nombre"].ToString();
persona.Pagos.Monto = Convert.ToDecimal(dr["Monto"]);
persona.Cliente.Comision = Convert.ToInt32(dr["Comision"]);
persona.Pagos.Autorizacion = Convert.ToBoolean(dr["Autorizacion"]);
persona.Pagos.Fecha= Convert.ToDateTime( dr["Fecha"]);

lstPagos.Add(persona);

By default when you create Modelo , your property Payments will be created with value null , if you want to alter this behavior you should modify the Modelo constructor:

public class Modelo{
    public Pagos Pagos { get; private set; }
    public Cliente Cliente { get; private set; }

    //Modifico el constructor
    public Modelo(){
        Pagos = new Pagos();
        Cliente = new Cliente();
    }
}

// y con esto ya podrias hacer lo que hiciste en primera instancia

Modelo persona = new Modelo();
persona.Pagos.IdPago = Convert.ToInt16(dr["Id_pago"]);
persona.Pagos.IdCliente = Convert.ToInt16(dr["Id_cliente"]);
persona.Cliente.Nombre = dr["Nombre"].ToString();
persona.Pagos.Monto = Convert.ToDecimal(dr["Monto"]);
persona.Cliente.Comision = Convert.ToInt32(dr["Comision"]);
persona.Pagos.Autorizacion = Convert.ToBoolean(dr["Autorizacion"]);
persona.Pagos.Fecha= Convert.ToDateTime( dr["Fecha"]);

lstPagos.Add(persona);
    
answered by 21.07.2018 / 00:42
source