带列表的ViewModel类

时间:2018-09-26 13:13:57

标签: c# c#-4.0 entity-framework-6

嗨,我已经创建了一个这样的ViewModel类

public class ClassA
{
    public string A { get; set; }
    public string B { get; set; }
    public List<ClassC> c { get; set; }
    public List<ClassD> d { get; set; }
}
public class ClassC
{
    public string A1 { get; set; }
    public string B1 { get; set; }
}
public class ClassD
{
    public string A1 { get; set; }
    public string B1 { get; set; }
}

然后我在MainClass中添加了这样的值

public ClassA MainClass()
    {
        ClassA CA = new ClassA();
        CA.A = "abcd";
        CA.B = "efgh";

        ClassC CC = new ClassC();
        CC.A1 = "qwer";
        CC.B1 = "rewq";
        CA.c.Add(CC);

        ClassD CD = new ClassD();
        CD.A2 = "lkjh";
        CD.B2 = "aqws";
        return CA;
    }

所以我在

处出错
  

CA.c.Add(CC);

,错误为"object reference not set to an instance of an object"

无论返回哪种ViewModel,我都会将其与实际模型进行比较,所以我该怎么做。

0 个答案:

没有答案