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; }
 }
6
  • 1
    You need to create a ViewModel first (call it EventListModel) and have property List<Event> EventList. Once you populate your list of events from Datasource it should work. Commented Jun 14, 2012 at 21:49
  • create a view from my getevent() ??? Commented Jun 14, 2012 at 21:51
  • 1
    Add new class to your asp.net mvc application Model folder, you may call it "EventListModel". Inside it declare elements that you want to display in your View (for example: formName, status, EventList) Commented Jun 14, 2012 at 21:55
  • is that wat i was doing..i have model called event inside that model i have class called event like you see in the above code Commented Jun 14, 2012 at 22:00
  • Does your parseResponse call return data without error ? Also try not put your event list inside ViewBag. It might be the reason why you getting error. Commented Jun 14, 2012 at 22:02

1 Answer 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; }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

is the FromId and PrgramID are just examples
Yes, it is just example, you may skip them.
@Yaroslav Why did you approve this suggested edit...? It's clearly inappropriate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.