Default Value of Role in a dropdown

0

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; }
    }
    
asked by anonymous 13.06.2016 / 16:00

1 answer

0

In fact it will not work. You are writing ViewBag.Roles N times: one for each execution of foreach :

        foreach (var user in result)
        {
            user.Roles = userManager.GetRoles(user.UserID);
            ViewBag.Roles = new SelectList(db2.Roles, "id", "Name", user.Roles);
        };

The correct one is to write ViewBag once and set every DropDownList to 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.DisplayFor(modelItem => item.RoleId)
            @Html.DropDownListFor(modelItem => item.Roles,((IEnumerable<Role>)ViewBag.Roles).Select(option => new SelectListItem {
                Text = option.Name,
                Value = option.Id,
                Selected = (item != null) && (item.RoleId == option.Id)
            }), new { @class = "form-control" })
         </td>
     </tr>
}
    
13.06.2016 / 16:17