locked
Combine 2 Model class into One Model Calls RRS feed

  • Question

  • User2041008840 posted

    hello,

    I want to combine 2 Model Class into 1 Model class. 
    I am using Asp.net core WebApi 
    here is my Code

    If I use single model then model accepting input but when in combine model its not accepting any value showing null.
    I am giving input using postman but its showing null in model - 

    Json Input
    { "Date" : "2020-03-31T16:44:21.833", "Description":"", "VoucherNo":"2000", "AccountCode":"01036700001", "accountposting" :[ { "Accoutposting1":"Dr", "Amount":2, "AccountCode":"01036700001" }, { "Accoutposting1":"Cr", "Amount":2, "AccountCode":"01036700001" }] }
     public class CombineJournalVoucherViewModel
        {
            public JournalVoucherViewModel JournalVoucher { get; set; }
            public List<AccountingPosting1ViewModel> AccountingPostings { get; set; }
        }
    public class JournalVoucherViewModel
        {
          public int ID { get; set; }
          public string AccountCode { get; set; }
         public DateTime Date { get; set; } = DateTime.Now;
         public decimal Amount { get; set; } = 0;
          public string AccountType { get; set; }
          public string Narration { get; set; }
          public int AccountSubTypeID { get; set; }
          public string VoucherNo { get; set; }
          public string Description { get; set; }
        }
    public class AccountingPosting1ViewModel
        {
            public int ID { get; set; }
            public int FormID { get; set; }
            public int AccountID { get; set; }
            public int RefDocDetails { get; set; }
            public DateTime PostingDate { get; set; } = DateTime.Now;
            public string AccountPosting1 { get; set; }
            public decimal Amount { get; set; } = 0;
            public string AccountCode { get; set; }
            public string Narration { get; set; }
            public string Description { get; set; }
            public string balance { get; set; } = null;
        }
    
    
     [HttpPost]
            public async Task<ActionResult<JournalVoucher>> PostJournalVoucher(CombineJournalVoucherViewModel  journalVoucher)
            {
                var jv = journalVoucher;
                    var chartofaccount = await _context.ChartOfAccount.Where(a => a.AccountCode == journalVoucher.JournalVoucher.AccountCode).FirstOrDefaultAsync();
                if (journalVoucher.JournalVoucher.ID <= 0)
                {
                    var journalvoucher = new JournalVoucher
                    {
                        CreatedOn = journalVoucher.JournalVoucher.Date,
                        VoucherNo = journalVoucher.JournalVoucher.VoucherNo,
                        Description = journalVoucher.JournalVoucher.Description,
                    };
    
                    _context.JournalVoucher.Add(journalvoucher);
                    await _context.SaveChangesAsync();
                    var RefDoc = journalvoucher.ID;
                    var FormDetail = await _context.FormDetails.Where(a => a.Name == "JV").FirstOrDefaultAsync();
    
                    List<AccountingPosting1ViewModel> accountingPostings = new List<AccountingPosting1ViewModel>();
                    accountingPostings = await _context.AccountPosting.Include(A => A.ChartOfAccount).Where(a => a.ChartOfAccount.AccountCode == journalVoucher.JournalVoucher.AccountCode).Select(a => new AccountingPosting1ViewModel
                    {
                        AccountPosting1 = a.AccountPosting1,
                        Amount = a.Amount,
                        AccountCode = a.ChartOfAccount.AccountCode
                    }).ToListAsync();
                    foreach (var i in accountingPostings)
                    {
                        var chartofAccount = await _context.ChartOfAccount.Where(a => a.AccountCode == i.AccountCode).FirstOrDefaultAsync();
                        var accountPostingDr = new AccountPosting
                        {
                            FormID = FormDetail.ID,
                            ChartOfAccountID = chartofAccount.ID,
                            PostingDate = i.PostingDate,
                            AccountPosting1 = i.AccountPosting1,
                            Amount = i.Amount,
                            RefDocDetails = RefDoc,
                            Description = i.Description,
                        };
                        _context.AccountPosting.Add(accountPostingDr);
                        await _context.SaveChangesAsync();
    }
    }

    Thursday, November 12, 2020 7:43 AM

All replies

  • User475983607 posted

    The JSON shared above has the following class structure.

        public class Journal
        {
            public DateTime Date { get; set; }
            public string Description { get; set; }
            public string VoucherNo { get; set; }
            public string AccountCode { get; set; }
            public Accountposting[] accountposting { get; set; }
        }
    
        public class Accountposting
        {
            public string Accoutposting1 { get; set; }
            public int Amount { get; set; }
            public string AccountCode { get; set; }
        }

    Let Visual Studio generate the classes.  Copy the JSON -> Edit -> Paste Special -> Paste JSON As Classes.

    Thursday, November 12, 2020 1:13 PM