2012-06-14 2 views
0

내 개체의 인스턴스에 문제가있어 그것을 나에게 내가 HTMLNullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다. foreach는 루프

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

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

그래서 내가 이런 짓을 이해하지 못하고있는 오류를했다. 이제는 [NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.]라고 말하고 내 <% 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; } 
} 
+1

당신은 먼저 뷰 모델을 만들어야합니다 (EventListModel 호출) 재산 목록을 이벤트 목록이있다. 데이터 소스에서 이벤트 목록을 채우면 제대로 작동합니다. –

+0

내 getevent()에서보기 만들기 ??? – Yusuf

+1

asp.net mvc 응용 프로그램 모델 폴더에 새 클래스를 추가하면 "EventListModel"이라고 할 수 있습니다. 내부에보기에 표시하려는 요소를 선언하십시오 (예 : formName, status, EventList) –

답변

3

모두와 클래스가, 샘플처럼 보일 수 있습니다. 또한 액션이 뷰 모델을 사용

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

은 FromId이고 PrgramID는 단지 예일뿐입니다. – Yusuf

+1

네, 단지 예일뿐입니다. 건너 뛸 수 있습니다. –

+0

그냥 위쪽 화살표를 클릭하면됩니까? – Yusuf

관련 문제