【问题标题】:NullReferenceException: Object reference not set to an instance of an object. in a foreach loopNullReferenceException:对象引用未设置为对象的实例。在 foreach 循环中
【发布时间】:2012-06-14 21:44:16
【问题描述】:

我的对象实例有问题,它给了我一个我不明白的错误

List<Event> events = parseResponse.Deserialize<List<Event>>(_responseAsString);
ViewBag.eventss = events;

html

<table id="eventist" border="0" cellspacing="0" cellpadding="0">
<thead>
    <tr>
        <th>
            event_key
        </th>
         <th>
            user_token
        </th>                
        <th>
            event_set_key
        </th>           
        <th>
            event_type
        </th>
         <th>
          event_date
        </th>
          <th>
            event_amount
        </th>
         <th>
            event_location_key
        </th>                
        <th>
            event_location_name
        </th>           
        <th>
            event_location_city
        </th>
         <th>
          event_location_state
        </th>
        <th>
            event_location_country
        </th>
         <th>
          event_acknowledged
        </th>
    </tr>
</thead>
<tbody>
<%List<StopMalaria.Models.Event> events= ViewBag.eventss;%>
<% foreach (var item in events)
   { %>

    <tr>
        <td>
            <%: item.event_key%>
        </td>
        <td>
              <%: item.user_token%>
        </td>               
        <td>
            <%: item.event_set_key%>
        </td>           
        <td>
            <%: item.event_type%>
        </td> 
         <td>
            <%: item.event_date%>
        </td>
        <td>
              <%: item.event_amount%>
        </td>               
        <td>
            <%: item.event_location_key%>
        </td>           
        <td>
            <%: item.event_location_name%>
        </td> 
         <td>
            <%: item.event_location_city%>
        </td>
        <td>
              <%: item.event_location_state%>
        </td>               
        <td>
            <%: item.event_location_country%>
        </td>           
        <td>
            <%: item.event_acknowledged%>
        </td>                   
    </tr>

<% } %>
</tbody>
</table>

所以我这样做了。现在它说 [NullReferenceException: Object reference not set to an instance of an object.] 和我的 &lt;% foreach(var item in events) 以红色突出显示。

我已经有一个包含所有元素的类

   public class Event
   {
     public string event_key { get; set; }
     public string user_token { get; set; }
     public string event_set_key { get; set; }
    public string event_type { get; set; }
    public string event_date { get; set; }
    public string event_amount { get; set; }
    public string event_location_key { get; set; }
    public string event_location_name { get; set; }
    public string event_location_city { get; set; }
    public string event_location_state { get; set; }
    public string event_location_country { get; set; }
    public string event_acknowledged { get; set; }
 }

【问题讨论】:

  • 您需要先创建一个 ViewModel(称为 EventListModel)并拥有属性 List EventList。从数据源填充事件列表后,它应该可以工作。
  • 从我的 getevent() 创建一个视图???
  • 将新类添加到您的 asp.net mvc 应用程序模型文件夹中,您可以将其称为“EventListModel”。在其中声明要在视图中显示的元素(例如:formName、status、EventList)
  • 那是我在做什么..我在该模型中有称为事件的模型我有称为事件的类,就像您在上面的代码中看到的那样
  • 您的 parseResponse 调用是否返回数据而没有错误?也尽量不要把你的事件列表放在 ViewBag 中。这可能是您收到错误的原因。

标签: c# .net html asp.net-mvc


【解决方案1】:

为您的视图添加视图模型,示例可能如下所示。在您的操作中也使用此视图模型:

 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; }
    }

【讨论】:

  • 是FromId,PrgramID只是例子
  • 是的,这只是示例,您可以跳过它们。
  • @Yaroslav 你为什么批准this suggested edit...?这显然是不合适的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2016-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 2020-11-17
相关资源
最近更新 更多