-1
I have in my index users, each user has a Dropdown with the Roles, when 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; }
}
My Mvc does not know the "Roll" argument inside a Ienumerable. Why it will be?
– Simão Lopes
Something’s wrong there. I wouldn’t put
public ICollection<String> Roles { get; set; }
within theViewModel
. Would placepublic String RoleId { get; set; }
.– Leonel Sanches da Silva
The Roles is a "Icollection" of the Users, or if I modify the tables of my Identity, to become a user corresponding to a Role.
– Simão Lopes
Well, then the modeling of View is all wrong anyway. See the articles on Begincollectionitem to do this part.
– Leonel Sanches da Silva