代码之家  ›  专栏  ›  技术社区  ›  Mark

从数据库模型填充视图模型但获取对象引用错误

  •  1
  • Mark  · 技术社区  · 12 年前

    我在使用字典时也遇到了类似的问题——现在我正试图填充一个视图模型,以向GET请求返回一个JSON对象。

    我的视图模型是:

    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

    首先,你能帮我解决这个错误吗?如果可能的话,确认我从数据库中读取和填充视图模型的方式是否是最好的方法?

    谢谢你,马克

    1 回复  |  直到 12 年前
        1
  •  4
  •   Henk Mollema    12 年前

    这是因为 hotels 属性从未初始化。您可以在的构造函数中执行此操作 HotelInventoryta :

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

    现在您用一个空集合初始化了属性,这样您就可以向其中添加项,而不是 酒店 存在 null 这会导致您的异常。