I want that in the register that comes by default when creating a project in MVC5, under repeat password, put a selection list ( ComboBox
) where it shows me the list of roles that exist in the table AspNetRoles
that Visual Studio creates by default.
I've had problems getting the list of the AspNetRoles
, since I don't know which context is using the Idendity
, and I don't know exactly how to create the select
.
I just need to know how to get the listing in the controller and how to pass it to the view, I
Insert
don't need it.
My Controller:
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
return View();
}
My sight:
@model StyleTest.Models.RegisterViewModel
@{
ViewBag.Title = "Registrarse";
}
<h2>@ViewBag.Title.</h2>
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Cree una cuenta nueva.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Registrarse" />
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
My connection in the web.config:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=StyleTest;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="StyleTestEntities" connectionString="metadata=res://*/Contexto.StyleTest.csdl|res://*/Contexto.StyleTest.ssdl|res://*/Contexto.StyleTest.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=StyleTest;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
I use MVC5 with Visual Studio 2015.
It's never a bad idea to read the documentation about Identity and how it works internally. With this you would know that Identity has its own context relative to the application (ApplicationDbContext) . In fact, if you enable migrations on a newly generated project by VS you'll see that it asks to specify which DbContext you're targeting the migration to (this is because you can "extend" IdentityModels properties). In the official documentation you will be able to see the reasons and why of all this.
First, inside your controller you must make certain definitions:
With this you assign the
ApplicationDbContext()
to a variableuserContext
, what do you achieve with this?... accessing the Identity API as if it were an ordinary DbContext. You will be able to see the classes Roles, Users, etc. and make the queries via LINQ that you deem convenient (among them you assign the roles to an enumerable to pass them to your SelectList).It would be something like this (I get roles/users and user in particular):
Then to pass to SelectList (in this case via ViewBag, use whatever you want, you can even send this data in JSON to exploit it in a JS front):
The select that you assign to that ViewBag via Razor will have as
value
the ID of the role and asoption
the name of the role.In your POST method for the record (which you will have to use a viewModel yes or yes to be able to add this new field correctly) you can assign the role as follows:
You must use
UserManager
to be able to get the user ID, in the initial implementation it is already initialized so you can use them directly.I have created a NuGet package that generates a controller and views with everything necessary for creating, assigning, editing and deleting roles to different users. It's for MVC5 and the views are for Bootstrap:
https://www.nuget.org/packages/ManageUsersProsmart/
You install it and that's it, it can also serve as an example. Greetings.
The context must be instantiated first:
Then add this at
register
or where you need:Finally, in your view you must put the following so that it reflects the data you need:
That is all.