list add return null exception

时间:2018-02-17 04:20:21

标签: c# .net model-view-controller

我有一个包含列表的类,如下所示。

namespace joerdie.Models.RANKD
{
    [Serializable]
    public class RANKDModel
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public List<thingsModel> thingList { get; set; }
    }
}

thingList类如下。

namespace joerdie.Models.RANKD 
{
    public class thingsModel
    {
        public int thingID { get; set; }
        public string thingName { get; set; }
        public string thingImgPointer { get; set; }
    }
}

我遇到的问题是当我尝试在控制器中添加到此列表时,出现空异常错误。 Controller代码如下。

public ActionResult Index(int id)
{
    RANKDModel model = new RANKDModel();

    var tblThingGroup = db.tblThingGroup
            .Where(x => x.thingCollectionID == id)
            .Include(t => t.tblThing)
            .Include(t => t.tblThingCollection)
            .OrderBy(x => x.displayOrder);

    foreach (var thing in tblThingGroup)
    {
        thingsModel things = new thingsModel();

        things.thingID = thing.tblThing.thingID;
        things.thingName = thing.tblThing.thingName;
        things.thingImgPointer = thing.tblThing.thingImgPointer;

        model.thingList.Add(things);
    }

    return View(model);
}

为什么我会收到此null异常错误?我正在填写该类的所有必填字段。

0 个答案:

没有答案