Role Default Value in a dropdown

Asked

Viewed 31 times

-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; }
    }

1 answer

0

In fact it won’t work. You’re 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 right thing is for you to write to ViewBag once and define each DropDownList in 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>
}
  • My Mvc does not know the "Roll" argument inside a Ienumerable. Why it will be?

  • Something’s wrong there. I wouldn’t put public ICollection<String> Roles { get; set; } within the ViewModel. Would place public String RoleId { get; set; }.

  • 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.

  • Well, then the modeling of View is all wrong anyway. See the articles on Begincollectionitem to do this part.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.