null引用添加到Fileinfo列表

时间:2016-12-01 16:26:12

标签: c# model-view-controller null

我有一个包含FileInfo对象列表的模型:

...
public enum type {PDF, CDR};
public class searchViewModel
{
    public string search { get; set; }
    public type type { get; set; }
    public List<FileInfo> results { get; set; }
}

然后我有一个调用服务的控制器方法并返回FleInfo对象列表:

public ActionResult Search(searchViewModel model)
    {
        FileService fi = new FileService();
        var files = fi.GetLastDone(model.search.ToString(), model.type.ToString());
        foreach (FileInfo file in files)
        {
            model.results.Add(file);
        }

        return View(model);
    }

但是这会在foreach循环内的行上产生Null引用异常:

Object reference not set to an instance of an object...
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 26:             foreach (FileInfo file in files)
Line 27:             {
Line 28:                 model.results.Add(file);
Line 29:             }
Line 30:                        

任何帮助将不胜感激!

0 个答案:

没有答案