Marius Vasile

Marius Vasile

  • 590
  • 1.9k
  • 146.2k

ASP.NET Core Razor Post "Value" and "Text" from ddl

Jun 12 2020 2:53 AM
I want to update User Roles by using a select and post it to db. The select is working fine for RoleName but when I change or add a role, the RoleId has to be updated too. How do I do that in Razor pages?
With the below code only the RoleName is updated, for RoleId I get error because its valuue is null 
 
  1. UserRoleClassName = await _context.Roles.Select(a => new SelectListItem  
  2.                                                         {  
  3.                                                             Value = a.Id.ToString(),  
  4.                                                             Text = a.Name  
  5.                                                         }).ToListAsync();  
  1. <div class="col-3">  
  2.                 <label for="RoleName">Role Name</label>  
  3.                 <select name="Role" class="form-control" asp-for="AppIdentityRole.Role" asp-items="Model.UserRoleClassName"></select>  
  4.             </div>  
  5.             <input hidden name="RoleId" asp-for="AppIdentityRole.RoleId" class="form-control" value="@Model.UserRoleClassName" />  
 

Answers (1)