2015-11-21 50 views
1

我在MongoLab上建立了一个数据库,该数据库被查询并解析为模型。该模型中的集合反过来又绑定到数据网格。但是,当我查询数据库时,网格上显示的唯一数据就是文档的对象ID。如何将Observable Collection上的空数据解析为数据网格绑定?

为了调试问题,我用每个字段的常量数据初始化列表,并且绑定起作用,填充网格上的每个字段。

然后导致我检查如何将数据映射到模型。

然后我逐步浏览了从服务器查询返回的Observable集合的内容。

这表明所有数据都正在返回,但所有模型字段都为空。而是创建一个客户数组,并将字段填充到不同的客户对象中。

有谁知道我可以如何进一步调试?

首先我检查了从查询返回的集合的内容。这说明空模型领域和客户的数组:

step 1

然后我检查了customers客户数组的内容(被填充):

step 2

文档JSON是在MongoLab中定义,然后映射到应用CustomerModel中的CustomerCollection:

http://hastebin.com/ipatatoqif.pl

CustomerModel:

public class CustomerModel : INotifyPropertyChanged 
{ 

    private ObjectId id; 
    private string firstName; 
    private string lastName; 
    private string email; 

    [BsonElement] 
    ObservableCollection<CustomerModel> customers { get; set; } 


    /// <summary> 
    /// This attribute is used to map the Id property to the ObjectId in the collection 
    /// </summary> 
    [BsonId] 
    public ObjectId Id 
    { 
     get 
     { 
      return id; 
     } 
     set 
     { 

      id = value; 
     } 
    } 

    [BsonElement("firstName")] 
    public string FirstName 
    { 
     get 
     { 
      return firstName; 
     } 
     set 
     { 
      firstName = value; 
      RaisePropertyChanged("FirstName"); 
     } 
    } 

    [BsonElement("lastName")] 
    public string LastName 
    { 
     get 
     { 
      return lastName; 
     } 
     set 
     { 
      lastName = value; 
      RaisePropertyChanged("LastName"); 
     } 
    } 

    [BsonElement("email")] 
    public string Email 
    { 
     get 
     { 
      return email; 
     } 
     set 
     { 
      email = value; 
      RaisePropertyChanged("Email"); 
     } 
    } 


    public event PropertyChangedEventHandler PropertyChanged; 
    private void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

这是示出在网格上,只有的ObjectID目前数据:

Orders data grid

回答

1

我不会MongoDB中直接存储的ObservableCollection<T>

相反,我将在MongoDB中存储一个List<T>

为什么? ObservableCollection<T>是WPF特定的数据结构,除非您使用write a custom serializer,否则可能不适用于MongoDB。

如果您使用MVVM,则需要将存储在MongoDB中的数据从ViewModel中分离出来。我建议从MongoDB中检索数据,然后使用映射器(如AutoMapperExpressMapper)将其映射到您的ViewModel。

请参阅another person who ran into the same problem

+1

问题在于我将解析的数据映射到模型中的列表。相反,我删除了模型中的列表,并将数据从模型返回到DataRepo类中的列表。链接到两个,模型:https://github.com/BrianJVarley/MongoDB_App/blob/master/MongoDBApp/Models/CustomerModel.cs回购:https://github.com/BrianJVarley/MongoDB_App/blob/master/MongoDBApp/DAL/CustomerRepository。cs –

+0

非常好,很高兴你解决了这个问题!可能是一个好主意,将其作为“更新”添加到您的排队的底部,以便其他用户可以看到您如何解决它。 – Contango