0

I have a User model.

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; }
}

This is is my UserGroup model that would contain user groups like Admin, System Admin, etc.

public class UserGroup
{
    public int Id {get; set;}
    public bool Admin {get; set;}
    public bool SystemAdmin {get; set}
    public List<User> Users {get; set;}
}

I just started .net core so I hope my relationship is correct. What I would like is to have a User Create View that had a html selection that list all the groups. Something like this but being dynamic.

<select class="form-control m-b" asp-for="Role" required>
    <option value=""></option> 
    <option value="Admin">Admin</option>
    <option value="SystemAdmin">System Admin</option>
</select>

This is the UserGroupViewModel that I'm send to the User Create Form.

public class UserCreateViewModel
{
    [Required]
    public string FirstName { get; set; }
    [Required]
    public string LastName { get; set; }
    [Required]
    public List<UserGroup> UserGroups {get; set;}
}

Finally on my UserController I'm going to be looping the List of UserGroups that I got from my database and I'll add it to the UserCreateViewModel.

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

        var userGroups = db.UserGroups.ToList();

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


        return View(userCreateViewModel);
    }

The problem is that I'm getting an error that I'm not sure how to deal with it.

enter image description here What does this error means and How can I solve it. I'm also open for a different approach from experience developers.

Millenial2020
  • 2,465
  • 9
  • 38
  • 83
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Tseng Aug 18 '17 at 19:37

3 Answers3

2

You are storing groups in a local variable but you need to set the UserGroups property on the instance of UserCreateViewModel which you created at start, so just change the following:

var userGroups = db.UserGroups.ToList();

to :

userCreateViewModel.UserGroups = db.UserGroups.ToList();

and you can skip the loop that you are doing down there, because we have already set the UserGroups collection with the result which was needed.

Hope it helps!

Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
2

Your code is crashing with a null reference exception because you are trying to call the Add method on NULL . When you create UserCreateViewModel object, you are not initializing the UserGroups property to an empty list and you do not have a constructor which does the same. So basically UseGroups property is null.

The solution is to initialize the UserGroups property before calling Add method on that.

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);
}

Or yo can directly set the collection to that property.

var userCreateViewModel = new UserCreateViewModel 
{
   UserGroups= db.UserGroups.ToList()
};
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

userCreateViewModel.UserGroups = userGroups;

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

    var userGroups = db.UserGroups.ToList();

    userCreateViewModel.UserGroups = userGroups;

    return View(userCreateViewModel);
}
DOMZE
  • 1,369
  • 10
  • 27