-1

I have a models like this:

   public class VMDetallePeriodoPAgo
    {
        public DetallePeriodoPagoViewModel Modelo1 { get; set; }
        public PerspectivaRealViewModel Modelo2 { get; set; }
    }

    public class DetallePeriodoPagoViewModel
    {
        public int ID { get; set; }

        public int IdRecibo { get; set; }

        public string Descripcion { get; set; }

        public int IdPeriodoPago { get; set; }

        public string Empleado { get; set; }

        public decimal Ingresos { get; set; }

        public decimal MontoISR { get; set; }
    }

    public class PerspectivaRealViewModel
    {
        public decimal PersepcionesReales { get; set; }
    }

And in controller I want to fill it as:

List<VMDetallePeriodoPAgo> LDPVM = new List<VMDetallePeriodoPAgo>();
            foreach (var i in periodos)
            {
                VMDetallePeriodoPAgo DPVM = new VMDetallePeriodoPAgo();

                DPVM.Modelo1.IdRecibo = i.IdRecibo;
                DPVM.Modelo1.Empleado = i.Empleado.Codigo;
                DPVM.Modelo1.Descripcion = i.Descripcion;
                DPVM.Modelo1.MontoISR = i.MontoIRS;
                LDPVM.Add(DPVM);
            }

Problem is when I try to execute it, when trying to fill model in line:

 DPVM.Modelo1.IdRecibo = i.IdRecibo;

I always getting:

Object reference not set to an instance of an object.

Why it happen, am I doing something wrong with my modeling? Regards

David
  • 795
  • 1
  • 5
  • 10
  • In VMDetallePeriodoPAgo constructor make sure you create a new instance as public VMDetallePeriodoPAgo() { Modelo1 = new DetallePeriodoPagoViewModel(); } if not withing foreach make sure you create a new instance before assignment like DPVM.Modelo1 = new DetallePeriodoPagoViewModel(); DPVM.Modelo1.IdRecibo = i.IdRecibo; DPVM.Modelo1.Empleado = i.Empleado.Codigo; DPVM.Modelo1.Descripcion = i.Descripcion; DPVM.Modelo1.MontoISR = i.MontoIRS; LDPVM.Add(DPVM); – Viju May 02 '18 at 18:04

3 Answers3

2

You should initialize the child object before you can use it:

Update the code in DPVM object initialization as

VMDetallePeriodoPAgo DPVM = new VMDetallePeriodoPAgo() {Modelo1 = new DetallePeriodoPagoViewModel() , Modelo2 = new PerspectivaRealViewModel() };

When an object is instantiated, it is allocated with a block of memory and configured as per the blueprint provided by the class underlying the object.

user1672994
  • 7,945
  • 1
  • 13
  • 26
1

Add this above your code:

DPVM.Modelo1 = new DetallePeriodoPagoViewModel(); //Added code.
DPVM.Modelo1.IdRecibo = i.IdRecibo;
Willy David Jr
  • 6,916
  • 3
  • 34
  • 45
-1

Your problem is not related to nesting or ViewModel. Modelo1 and Modelo2 was not initialized in code you provided.

You can try the following:

public class VMDetallePeriodoPAgo
{
    public DetallePeriodoPagoViewModel Modelo1 { get; set; }
    public PerspectivaRealViewModel Modelo2 { get; set; }

    public VMDetallePeriodoPAgo()
    {
            Modelo1 = new DetallePeriodoPagoViewModel();
            Modelo2 = new PerspectivaRealViewModel();
    }
}
Venantius
  • 2,274
  • 2
  • 24
  • 34