Insert user role automatically

0

I have this structure and I am working with ASP MVC. I want to make a user registration and what I want to do is that when the user registers, add a role automatically.

As you can see, there are different users so I just want to make a single form that works for all users and, depending on the type of user, that chooses to assign a role specifically when the record is saved.

What I want to do is, when saving the user's record, insert the value of the id of the role that is in the table rol automatically in the table Usuario , which is the one that is related .

    
asked by Kevin 02.10.2016 в 05:12
source

1 answer

0

What you can understand is that you want me to save you the Rol_id "automatically". But that id has to come from somewhere, as Mariano told you in the answer.

If you have a list in the registration form where you select the role, because the previous code works for you.


public ActionResult Create([Bind(Include = "id, Nombre, Correo, Password, Rol_id, IDTUSU")] Usuario usuario)
{
   if(ModelState.IsValid)
   {
     db.Usuario.Add(usuario);
     db.SaveChanges();                       
     return RedirectToAction("Index");
   }  
}

But if you want to assign them a default role and always the same, you just have to know the id of the Role that is registered in the database. And then add the following line before .add .


usuario.Rol_id = aca_va_el_id_del_rol
    
answered by 04.10.2016 в 23:44