my API
[HttpPost("AffectationRoleToUser")]
public async Task<IActionResult> AssignRoleToUser(int userId, int roleId)
{
try
{
var user = await _context.Users.FindAsync(userId);
var role = await _context.Roles.FindAsync(roleId);
if (user == null)
return NotFound(new { Message = "User not found!" });
if (role == null)
return NotFound(new { Message = "Role not found!" });
var affectationRole = new AffectationRole
{
User = user,
Role = role,
DateAffectation = DateTime.Now
};
_context.AffectationRoles.Add(affectationRole);
await _context.SaveChangesAsync();
return Ok(new { Message = "Role assigned to user successfully!" });
}
catch (Exception ex)
{
// Log the exception
Console.WriteLine($"An error occurred while assigning role to user: {ex.Message}");
return StatusCode(500, new { Message = "An error occurred while assigning role to user. Please try again later." });
}
}
and this is my page, my question is, how to or what logic can I use to assign these roles to all these users at once.
(https://i.stack.imgur.com/kHO4I.png)
i’ve tried to store the ID of the user and the ID of the role in a list then send them but it didn’t work
New contributor
Fedi Berrima is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.