C# 如何在.net core中将表列表添加到ViewModel中

C# 如何在.net core中将表列表添加到ViewModel中,c#,asp.net,.net,asp.net-mvc,asp.net-core,C#,Asp.net,.net,Asp.net Mvc,Asp.net Core,我有一个用户模型 public class User { public int Id { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } [Required] public int UserGroupId { get; set; } public UserGroup

我有一个用户模型

public class User
{
    public int Id { get; set; }
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public int UserGroupId { get; set; }
    public UserGroup UserGroup { get; set; }
}
这是我的用户组模型,它将包含诸如Admin、System Admin等用户组

public class UserGroup
{
    public int Id {get; set;}
    public bool Admin {get; set;}
    public bool SystemAdmin {get; set}
    public List<User> Users {get; set;}
}
问题是我遇到了一个错误,我不知道如何处理它


这个错误意味着什么?我如何解决它。我也愿意采用与经验开发人员不同的方法。

您将组存储在局部变量中,但需要在开始时创建的
UserCreateViewModel
实例上设置
UserGroups
属性,因此只需更改以下内容:

var userGroups = db.UserGroups.ToList();
致:

您可以跳过下面的循环,因为我们已经用所需的结果设置了
UserGroups
集合


希望有帮助

您正在本地变量中存储组,但需要在开始时创建的
UserCreateViewModel
实例上设置
UserGroups
属性,因此只需更改以下内容:

var userGroups = db.UserGroups.ToList();
致:

您可以跳过下面的循环,因为我们已经用所需的结果设置了
UserGroups
集合


希望有帮助

userCreateViewModel.UserGroups=用户组

public IActionResult Create()
{
    UserCreateViewModel userCreateViewModel = new UserCreateViewModel();

    var userGroups = db.UserGroups.ToList();

    userCreateViewModel.UserGroups = userGroups;

    return View(userCreateViewModel);
}

userCreateViewModel.UserGroups=用户组

public IActionResult Create()
{
    UserCreateViewModel userCreateViewModel = new UserCreateViewModel();

    var userGroups = db.UserGroups.ToList();

    userCreateViewModel.UserGroups = userGroups;

    return View(userCreateViewModel);
}

由于您试图调用
null
上的
Add
方法,代码因null引用异常而崩溃。当您创建
UserCreateViewModel
对象时,您没有将
UserGroups
属性初始化为空列表,并且没有执行相同操作的构造函数。所以基本上,
UseGroups
属性是空的

解决方案是在调用
Add
方法之前初始化
UserGroups
属性

var userCreateViewModel = new UserCreateViewModel();
userCreateViewModel.UserGroups =new List<UserGroup>(); // Initialize to empty list

var userGroups = db.UserGroups.ToList();

foreach(var group in userGroups)
{
      userCreateViewModel.UserGroups.Add(group);
}

由于您试图调用
null
上的
Add
方法,代码因null引用异常而崩溃。当您创建
UserCreateViewModel
对象时,您没有将
UserGroups
属性初始化为空列表,并且没有执行相同操作的构造函数。所以基本上,
UseGroups
属性是空的

解决方案是在调用
Add
方法之前初始化
UserGroups
属性

var userCreateViewModel = new UserCreateViewModel();
userCreateViewModel.UserGroups =new List<UserGroup>(); // Initialize to empty list

var userGroups = db.UserGroups.ToList();

foreach(var group in userGroups)
{
      userCreateViewModel.UserGroups.Add(group);
}

谢谢,我喜欢新的解决方案更干净。谢谢,我喜欢新的解决方案更干净。谢谢,这是有意义的。谢谢,这是有意义的。可能的重复可能的重复确实!太快了。修正!;-)的确太快了。修正!;-)
var userCreateViewModel = new UserCreateViewModel();
userCreateViewModel.UserGroups =new List<UserGroup>(); // Initialize to empty list

var userGroups = db.UserGroups.ToList();

foreach(var group in userGroups)
{
      userCreateViewModel.UserGroups.Add(group);
}
var userCreateViewModel = new UserCreateViewModel 
{
   UserGroups= db.UserGroups.ToList()
};