从数据库模型填充视图模型,但收到对象引用错误



我在字典上遇到了类似的问题 - 现在我正在尝试填充视图模型,将 JSON 对象返回给 GET 请求。

我的观点模型是:

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

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

谢谢你,马克

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

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

现在,你使用空集合初始化了该属性,以便可以向其添加项,而不是hotels null从而导致异常。

相关内容

  • 没有找到相关文章

最新更新