控制器中的异常NullReferenceException(asp.net mvc)

时间:2013-06-07 06:24:09

标签: asp.net-mvc asp.net-mvc-4

我的项目中有供应商模型和用户模型,每个供应商都有一些用户 供应商模型

public class SupplierRow
{
    public Guid Id { get; set; }
    public string FullName { get; set; }
    public bool Subscribed { get; set; }
    public bool Active { get; set; }
    public int Visits { get; set; }

    public List<UserRow> Users { get; set; }

    public bool AllInactive
    {
        get
        {
            foreach (UserRow ur in Users)
            {
                if (ur.Status == 1) return false;
            }
            return true;
        }
    }
}

和用户模型

public class UserRow
{
    public Guid Id { get; set; }
    public string FullName { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Status { get; set; }
    public int Role { get; set; }

    public Guid SupplierId { get; set; }
}

然后我在控制器中使用我的模型

 public ActionResult Grid(bool? active)
    {
        var suppliers = Context.Suppliers.AsNoTracking()
            .WhereIf(active != null, e => e.Active == active)
            .Select(e => new SupplierRow
                            {
                                Id = e.Id,
                                FullName = e.FullName,
                                Active = e.Active,
                                Visits = e.Visits,
                            })
                            .ToList();


        List<Guid> supplierIds = new List<Guid>();
        foreach (SupplierRow sr in suppliers)
        {
            supplierIds.Add(sr.Id);
        }

        var users = Context.Users.AsNoTracking()
            .Where(e => supplierIds.Contains(e.SupplierId.Value))
            .Select(e => new UserRow
            {
                Id = e.Id,
                FullName = e.FullName,
                Email = e.Email,
                Name = e.Name,
                Status = e.Status,
                Role = e.Role,
                SupplierId = e.SupplierId.Value
            }).ToList();


        foreach (UserRow ur in users) 
        {
            foreach (SupplierRow sr in suppliers) 
            {
                if (ur.SupplierId == sr.Id) 
                {
                    sr.Users.Add(ur);
                } 
            }
        }

        return PartialView("_Grid", suppliers);
    }

但是当我尝试调试我的项目时,我在这里得到了一些例外 enter image description here 怎么了?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

您的Users列表未初始化。在访问之前创建新列表Users = new List<UserRow>();您可以更改SupplierRow类:

public class SupplierRow {
    private List<UserRow> users = new List<UserRow>();

    public List<UserRow> Users
    {
        get { return users; }
        set { users = value; }
    }
    ...
}

或在构造函数中:

public class SupplierRow
{
    public SupplierRow()
    {
        Users = new List<UserRow>();
    }
    public List<UserRow> Users { get; set; }
    ...
}

或在访问之前:

foreach (UserRow ur in users) 
{
    foreach (SupplierRow sr in suppliers) 
    {
        sr.Users = new List<UserRow>();
        if (ur.SupplierId == sr.Id) 
        {
            sr.Users.Add(ur);
        } 
    }
}

或者您可以使用linq:

foreach (SupplierRow sr in suppliers) 
{
    sr.Users = users.Where(user => user.SupplierId == sr.Id);
}

return PartialView("_Grid", suppliers);