My application is based on using the primary key type int in Aspnet Identity as described here . The problem is when it comes to creating a user. In the original template, I added a property in the RegisterViewModel, associated the Roles in a DropDownList, and it was ready. Now I'm having trouble
Here's a Model:
public class RegisterViewModel
{
[Required]
[Display(Name = "UserRoles")]
public int UserRoles { get; set; }
[Required]
[EmailAddress]
[Display(Name = "Email")]
public string Nome { get; set; }
...
}
The Controller
public ActionResult Register()
{
ViewBag.Name = new SelectList(context.Roles.ToList(), "Id", "Name");
return View()
}
A View Register.cshtml
<!--Selecione o Tipo de Perfil para o usuário-->
<div class="col-md-6">
@*@Html.Label("Selecione o Tipo de Perfil do Usuário")
@Html.DropDownList("UserRoles", (SelectList)ViewBag.Name, "Selecione o perfil de usuário", new { @class = "form-control" })
</div>
<!--termina aqui-->
Roles are displayed in the registry:
IntheRegisterPostthecodeisthis:
if(ModelState.IsValid){varuser=newApplicationUser{UserName=model.Email,Email=model.Email,Nome=model.Nome,Nacionalidade="brasileira", EstadoCivil = model.EstadoCivil, Identidade = model.Identidade, OrgaoExpedidor = model.OrgaoEmissor, CPF = model.CPF, Endereco = model.Endereco, Numero = model.Numero, Complemento = model.Complemento, Bairro = model.Bairro, Cidade = model.Cidade, Estado = model.Estado, DDD_Celular = model.DDD_Celular, Celular = model.Celular, DDD_TelComercial = model.DDD_TelComercial, TelComercial = model.TelComercial, DDD_Telefone = model.DDD_Telefone, DDD_WhatsApp = model.DDD_WhatsApp, PreferenciaContato = model.PreferenciaContato };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
...
Attempting to register gives the following error:
What might be causing this error?