I have in my index the users, each user has a Dropdown with Roles, when does Get does not show the value that is by default in Dropdown
Controller:
public ActionResult IndexGestor(string roles)
{
//query entre 2 bases de dados
var IDsCatequese = db.Catequese.ToList();
var nomeCatequese = db2.Users.ToList();
var result = (from o in IDsCatequese
join t in nomeCatequese on o.CatequeseID equals t.CatequeseID
//where t.Roles.Any(r => r.RoleId == t.Id)
select new CatequeseUsersViewModel
{
UserID = t.Id,
Nome = t.Nome,
UserName = t.UserName,
NomeCatequese = o.NomeCatequese,
CatequeseID = o.CatequeseID,
//Roles = t.Roles
}).ToList();
var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
foreach (var user in result)
{
user.Roles = userManager.GetRoles(user.UserID);
ViewBag.Roles = new SelectList(db2.Roles, "id", "Name", user.Roles);
};
return View(result.ToList());
}
View:
@foreach (var item in Model)
{
<tr> <td>
@Html.DisplayFor(modelItem => item.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.NomeCatequese)
</td>
<td>
@Html.DropDownListFor(modelItem => item.Roles,(IEnumerable<SelectListItem>)ViewBag.Roles, new { @class = "form-control" })
@Html.DisplayFor(modelItem => item.Roles)
}
ViewModel:
public class CatequeseUsersViewModel
{
public String UserID { get; set; }
public String NomeCatequese { get; set; }
public int CatequeseID { get; set; }
public String UserName { get; set; }
public String Paroquia { get; set; }
public String Nome { get; set; }
public ICollection<String> Roles { get; set; }
}