I find in a code example downloaded the following line of code in an Action method of a controller. ViewBag.RoleId = new SelectList(RoleManager.Roles, "Id", "Name") or probably its async version ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Id", "Name");
I am not able to fathom whats happening here. Also in the corresponding view, I nowhere see ViewBag.RoleId being used. Instead I find in the view @Html.DropDownList("RoleId","No Roles")
There appears to be some trivial connection between the two. Can someone throw some light at whet I am missing.
The following is the line of code in the action method.
public async Task<ActionResult> Create()
{
//Get the list of Roles
ViewBag.RoleId = new SelectList(await RoleManager.Roles.ToListAsync(), "Id", "Name");
return View();
}
And the corresponding view is as follows.
@model AspnetIdentitySample.Models.RegisterViewModel
@{
ViewBag.Title = "Create";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
</hgroup>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset class="form-horizontal">
<legend>Create a new user.</legend>
<div class="control-group">
@Html.LabelFor(m => m.UserName, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.UserName)
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.Password, new { @class = "control-label" })
<div class="controls">
@Html.PasswordFor(m => m.Password)
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "control-label" })
<div class="controls">
@Html.PasswordFor(m => m.ConfirmPassword)
</div>
</div>
<div class="control-group">
@Html.LabelFor(m => m.HomeTown, new { @class = "control-label" })
<div class="controls">
@Html.TextBoxFor(m => m.HomeTown)
</div>
</div>
<h4>Select Role for User</h4>
<hr />
@Html.DropDownList("RoleId","No Roles")
<div class="form-actions no-color">
<input type="submit" value="Create" class="btn" />
</div>
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}