0

I'm having trouble with the instance of my object it gave me an error in which i don't understand

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>

So i did this. now it is saying [NullReferenceException: Object reference not set to an instance of an object.] and my <% foreach(var item in events) are highlighted red .

I already have a class with all of the element

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

1 回答 1

3

Add a viewmodel for your view, sample may look like. Also use this viewmodel in your action:

 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; }
    }
于 2012-06-14T21:59:21.367 回答