模型不接受它的价值

时间:2015-03-20 06:09:47

标签: c# mvvm ms-access-2010

此代码不接受我为用户的部门数据提供的值。如果我用一个简单的int变量替换user.Department.ID,它工作正常。这也是user.Department.Code和user.Department.Title的问题。我不知道是什么问题。它在我的模特中吗?

这是代码。系统查询用户数据。我相信查询很好。我正在使用MS Access btw的连接字符串。

try
        {
            OleDbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                UserModel user = new UserModel();
                if (!reader.IsDBNull(0))
                {
                    user.Id = reader.GetInt32(0);
                }
                if (!reader.IsDBNull(1))
                {
                    user.Department.Id = reader.GetInt32(1);
                }
                if (!reader.IsDBNull(2))
                {
                    user.Department.Code = reader.GetString(2);
                }
                if (!reader.IsDBNull(3))
                {
                    user.Department.Title = reader.GetString(3);
                }
                if (!reader.IsDBNull(4))
                {
                    user.FamilyName = reader.GetString(4);
                }
                if (!reader.IsDBNull(5))
                {
                    user.GivenName = reader.GetString(5);
                }
                if (!reader.IsDBNull(6))
                {
                    user.MiddleName = reader.GetString(6);
                }
                if (!reader.IsDBNull(7))
                {
                    user.NameSuffix = reader.GetString(7);
                }
                if (!reader.IsDBNull(8))
                {
                    user.Type = reader.GetString(8);
                }
                CurrentUser = user;
                CanLogin = true;   
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Exception Error");
        }

这是我的Usermodel ......

class UserModel
    {
        public int Id { get; set; }
        public DepartmentModel Department { get; set; }
        public string FamilyName { get; set; }
        public string GivenName { get; set; }
        public string MiddleName { get; set; }
        public string NameSuffix { get; set; }
        public string Type { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
        public string FullName(bool isFamilyNameFirst)
        {
            StringBuilder fullname = new StringBuilder();
            if(isFamilyNameFirst)
            {
                fullname.Append(FamilyName).Append(", ").Append(GivenName).Append(" ").Append(MiddleName).Append(" ").Append(NameSuffix);
            }
            else
            {
                fullname.Append(GivenName).Append(" ").Append(MiddleName).Append(" ").Append(FamilyName).Append(" ").Append(NameSuffix);
            }
            return fullname.ToString();
        }
    }

我的部门模特..

class DepartmentModel
    {
        public int Id { get; set; }
        public InstitutionModel Institution { get; set; }
        public string Code { get; set; }
        public string Title { get; set; }
    }

1 个答案:

答案 0 :(得分:0)

Department属性永远不会初始化任何位置,你在用户构造函数中执行:

class UserModel
{
    public UserModel()
    {
        this.Department = new DepartmentModel();
    }

    public int Id { get; set; }
    public DepartmentModel Department { get; set; }
    public string FamilyName { get; set; }
    public string GivenName { get; set; }
    public string MiddleName { get; set; }
    public string NameSuffix { get; set; }
    public string Type { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string FullName(bool isFamilyNameFirst)
    {
        StringBuilder fullname = new StringBuilder();
        if(isFamilyNameFirst)
        {
            fullname.Append(FamilyName).Append(", ").Append(GivenName).Append(" ").Append(MiddleName).Append(" ").Append(NameSuffix);
        }
        else
        {
            fullname.Append(GivenName).Append(" ").Append(MiddleName).Append(" ").Append(FamilyName).Append(" ").Append(NameSuffix);
        }
        return fullname.ToString();
    }
}

UserModel user = new UserModel() {Department = new DepartmentModel()};