对象引用未设置为对象创建时的对象实例

时间:2018-03-11 04:46:53

标签: c#

我在MVC中使用EF从数据库中获取客户列表。但是,当我尝试将列表中每个项的值分配给模型类的对象时,我得到错误“对象引用未设置为对象的实例”。 在“CustomerRecord temp = new CustomerRecord();”行在以下代码中。

任何人都可以解释原因吗?

这是我的代码:

            foreach (var item in Customers)
            {

                CustomerRecord temp = new CustomerRecord();

                temp.CustomerName = item.Name;
                temp.Cell = item.RepCell;
                //temp.DebitBalance = 0;
                //temp.CreditBalance = 0;



                ReceivablePayable.CustomerRecords.Add(temp);
            }

这是我的模特

public class ReceivablePayableDTO
    {
        public DateTime Date { get; set; }

        [Display(Name = "Date")]
        public string TempDate { get; set; }
        public string Status { get; set; }
        public string ReportGenerationDate { get; set; }
        public List<CustomerRecord> CustomerRecords { get; set; }
    }

    public class CustomerRecord
    {
        public string CustomerName { get; set; }
        public string Cell { get; set; }
        //public Decimal DebitBalance { get; set; }
        //public Decimal CreditBalance { get; set; }
    }

0 个答案:

没有答案
相关问题