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.
What does this error means and How can I solve it. I'm also open for a different approach from experience developers.