1

我在字典中遇到了类似的问题 - 现在我正在尝试填充视图模型,以将 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

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

谢谢你,马克

4

1 回答 1

4

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

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

    // ...
}

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

于 2013-11-01T15:44:15.977 回答