locked
Combine Model not working RRS feed

  • Question

  • User2041008840 posted

    Hello, 

    I have combine model

     public class JvCombine
        {
            public JournalVoucherViewModel JournalVoucher { get; set; }
            public List<AccountPostingViewModel> AccountPosting { get; set; }
        
      public class JournalVoucherViewModel
        {
            public int ID { get; set; }
            public DateTime Date { get; set; }
    }
     public class AccountPostingViewModel
        {
            public int ID { get; set; }
            public DateTime PostingDate { get; set; }
    }
    JvCombine jvCombine = new JvCombine();
        JournalVoucherViewModel JournalVoucher { get; set; } = new JournalVoucherViewModel();

    I declare the obj for this class JournalVoucherViewModel as JournalVoucher
    and add value as Date into it and then set this value into 

    jvCombine.JournalVoucher.Date = JournalVoucher.Date;

    but this jvCombine.JournalVoucher.Date stays null dont accept value from JournalVoucher.Date 
    I checked using debugger it shows value JournalVoucher.Date but not getting into jvCombine.JournalVoucher.Date

    What will be the solution? Did I missed something?

    Friday, April 23, 2021 6:56 PM

All replies

  • User475983607 posted

    Below is a common pattern.

        public class JvCombine
        {
            public JournalVoucherViewModel JournalVoucher { get; set; }
            public List<AccountPostingViewModel> AccountPosting { get; set; }
        }
    
        public class JournalVoucherViewModel
        {
            public int ID { get; set; }
            public DateTime Date { get; set; }
        }
        public class AccountPostingViewModel
        {
            public int ID { get; set; }
            public DateTime PostingDate { get; set; }
        }

    See the C# programing guide to learn how to build classes.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes

    Friday, April 23, 2021 8:40 PM
  • User303363814 posted

    but this jvCombine.JournalVoucher.Date stays null dont accept value from JournalVoucher.Date
    It is very important to look closely at exact error messages.  Part of learning to program is to deeply understand error messages and what they mean.

    The error message would not say "null dont accept value".  I suspect that you have a NullReferenceException, but this is only a guess because you have not told us the precise error that you are encountering.

    If my guess is correct and you use the debugger you would be able to see that after the statement which initialises jvCombine - "new JvCombine()" - that the property of jvCombine called JournalVoucher is null.  When you create an instance of JvCombine it does not create an instance of JournalVoucherViewModel and store a reference to it in JournalVoucher.  You have to write your own code to do that.

    You could add a constructor to JvCombine which initialises all the properties to appropriate values.  Alternatively, at every place in your code where you create a new JvCombine you could then initialise the JournalVoucher and AccountPosting properties.

    If there are sensible defaults for these properties then I would favour the constructor approach rather than client code having to do it each time.  (There is a principal that after being constructed an object should be in a consistent state, my guess is that having null for these properties means that the object is not consistent)

    Sunday, April 25, 2021 7:26 AM