C# NullReferenceException:对象引用未设置为对象的实例。在foreach循环中
我的对象实例有问题,它给了我一个我不理解的错误C# NullReferenceException:对象引用未设置为对象的实例。在foreach循环中,c#,.net,html,asp.net-mvc,C#,.net,Html,Asp.net Mvc,我的对象实例有问题,它给了我一个我不理解的错误 List<Event> events = parseResponse.Deserialize<List<Event>>(_responseAsString); ViewBag.eventss = events; 为视图添加viewmodel,示例可能如下所示。在操作中也使用此viewmodel: public class EventListModel { public EventLis
List<Event> events = parseResponse.Deserialize<List<Event>>(_responseAsString);
ViewBag.eventss = events;
为视图添加viewmodel,示例可能如下所示。在操作中也使用此viewmodel:
public class EventListModel
{
public EventListModel()
{
EventList = new List<Event>();
}
public string FormId { get; set; }
public string ProgramId { get; set; }
public string FormName { get; set; }
public IList<Event> EventList { get; set; }
}
公共类事件列表模型
{
公共事件列表模型()
{
EventList=新列表();
}
公共字符串FormId{get;set;}
公共字符串程序ID{get;set;}
公共字符串FormName{get;set;}
公共IList事件列表{get;set;}
}
为视图添加视图模型,示例可能如下所示。在操作中也使用此viewmodel:
public class EventListModel
{
public EventListModel()
{
EventList = new List<Event>();
}
public string FormId { get; set; }
public string ProgramId { get; set; }
public string FormName { get; set; }
public IList<Event> EventList { get; set; }
}
公共类事件列表模型
{
公共事件列表模型()
{
EventList=新列表();
}
公共字符串FormId{get;set;}
公共字符串程序ID{get;set;}
公共字符串FormName{get;set;}
公共IList事件列表{get;set;}
}
您需要首先创建一个ViewModel(称为EventListModel)并拥有属性列表EventList。一旦您从数据源填充事件列表,它就应该可以工作。从my getevent()创建视图?将新类添加到asp.net mvc应用程序模型文件夹中,您可以将其称为“EventListModel”。在它里面声明你想在你的视图中显示的元素(例如:formName,status,EventList)就是我正在做的事情..我在模型里面有一个名为event的模型,我有一个名为event的类,就像你在上面的代码中看到的那样,你的parseResponse调用返回数据没有错误吗?另外,不要将事件列表放在ViewBag中。这可能是您出错的原因。您需要首先创建一个ViewModel(称为EventListModel)并具有属性列表EventList。一旦您从数据源填充事件列表,它就应该可以工作。从my getevent()创建视图?将新类添加到asp.net mvc应用程序模型文件夹中,您可以将其称为“EventListModel”。在它里面声明你想在你的视图中显示的元素(例如:formName,status,EventList)就是我正在做的事情..我在模型里面有一个名为event的模型,我有一个名为event的类,就像你在上面的代码中看到的那样,你的parseResponse调用返回数据没有错误吗?另外,不要将事件列表放在ViewBag中。这可能是您出错的原因。FromId和PrgramID只是示例吗?这只是示例,您可以跳过它们。@Yaroslav为什么批准。。。?这显然是不合适的。FromId和PrgramID只是个例子吗?这只是个例子,你可以跳过它们。@Yaroslav你为什么批准。。。?这显然不合适。
public class EventListModel
{
public EventListModel()
{
EventList = new List<Event>();
}
public string FormId { get; set; }
public string ProgramId { get; set; }
public string FormName { get; set; }
public IList<Event> EventList { get; set; }
}