2013-11-01 2 views
1

사전에 비슷한 문제가 있습니다. 이제 JSON 객체를 GET 요청으로 반환하기 위해 viewmodel을 채우려고합니다.데이터베이스 모델에서 뷰 모델을 채우지 만 객체 참조 오류가 발생했습니다

내 뷰 모델은 다음과 같습니다

public class HotelInventoryta 
{ 
    public int api_version { get; set; } 
    public string lang { get; set; } 
    public List<Hotel_List_ta> hotels { get; set; } 
} 

public class Hotel_List_ta 
{ 
    public int ta_id { get; set; } 
    public string partner_id { get; set; } 
    public string name { get; set; } 
    public string street { get; set; } 
    public string city { get; set; }  
    public string postal_code { get; set; } 
    public string state { get; set; } 
    public string country { get; set; } 
    public double latitude { get; set; }  
    public double longitude { get; set; } 
    public string desc { get; set; }  
    public string url { get; set; }  
    public string email { get; set; } 
    public string phone { get; set; } 
    public string fax { get; set; }  
} 

내 데이터베이스 모델은 다음과 같습니다 뷰 모델을 채우는

[Table("tblHotel")] 
public class Hotelta 
{ 
    [Key()] 
    [Column("hotel_id")] 
    public long hotel_id { get; set; } 
    public string hotel_name { get; set; } 
    public string hotel_add1 { get; set; } 
    public string hotel_towncity { get; set; } 
    public string hotel_pc { get; set; } 
    public string hotel_country { get; set; } 
    public string hotel_pass { get; set; } 
    public string hotel_email { get; set; } 
    public string hotel_tel { get; set; } 
    public string hotel_fax { get; set; } 
} 

내 컨트롤러 코드는 다음과 같습니다

private HoteltaContext dbh = new HoteltaContext(); 
    // 
    // GET: /ta/hotel_inventory 
    [HttpGet] 
    public HotelInventoryta hotel_inventory(int api_version, string lang) 
    { 
     { 

      HotelInventoryta hotelinventory = new HotelInventoryta(); 
      hotelinventory.api_version = api_version; 
      hotelinventory.lang = lang; 

      // Get data from database 
      var h = dbh.Hotelta.Where(x => x.hotel_id != 0).ToList(); 

      // loop through each result, and add it to the hotelinventory.hotels model 
      foreach (var ht in h) 
      { 
       // I get the exception on the next line 
       hotelinventory.hotels.Add(new Hotel_List_ta 
       { 
        ta_id = 0, 
        partner_id = ht.hotel_id.ToString(), 
        name = ht.hotel_name, 
        street = ht.hotel_add1, 
        city = ht.hotel_towncity, 
        postal_code = ht.hotel_pc, 
        country = ht.hotel_country, 
        url = "http://www.me.com", 
        email = ht.hotel_email, 
        phone = ht.hotel_tel, 
        fax = ht.hotel_fax 
       }); 
      } 

      return hotelinventory; 
     } 
    } 

오류 :

Object reference not set to an instance of an object

첫 번째로, 오류를 해결하는 데 도움이 될 수 있습니까? 가능한 경우 데이터베이스에서 읽는 방식과 viewmodel을 채우는 방식이 가장 적합한 방법인지 확인하십시오.

는 마크

+0

전체 스택 추적을 표시하면 매우 유용하다고 생각합니다. 또는 최소한 예외가 발생한 행을 알려주는 경우. – termit

답변

4

hotels 속성이 초기화되지 않습니다 때문입니다 감사합니다. 당신은 HotelInventoryta의 생성자에서이 작업을 수행 할 수 있습니다 : 당신이 hotels이 예외가 발생 null되는 것이 아니라, 여기에 항목을 추가 할 수 있도록

public class HotelInventoryta 
{ 
    public HotelInventoryta() 
    { 
     hotels = new List<Hotel_List_ta>(); 
    } 

    // ... 
} 

이제 빈 컬렉션 속성을 initialzed.

+0

화려한 - 고마워요! – Mark

관련 문제