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