Skip to main content
 首页 » 编程设计

c#之从数据库模型填充 View 模型但出现对象引用错误

2024年02月13日18telwanggs

我在使用字典时遇到了类似的问题 - 现在我正在尝试填充 View 模型,以将 JSON 对象返回到 GET 请求。

我的 View 模型是:

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

我填充 View 模型的 Controller 代码是:

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

错误是:

对象引用未设置为对象的实例

首先,您能帮我解决这个错误吗?如果可能,请确认我从数据库中读取数据并填充 View 模型的方式是否是最好的方式?

谢谢你,马克

请您参考如下方法:

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

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

现在您使用一个空集合初始化该属性,因此您可以向它添加项目,而不是 hotels 为 null 导致您的异常。