Search code examples
asp.net-coredrop-down-menuoptgroupselectlistitem

OptGroup in SelectListItem Not showing in DropDownList


I wrote a method to show Items and in Groups in DropDownlist with SelectListItem, But the problem is that only show the first group name and child plus the childs of other groups. The problem is that do not show second, third,.. groups (but show their childs). My model is

public class PermissionsViewModel
    {
        public long ID { get; set; }
        public string Title { get; set; }
        public long TypeId { get; set; }
        public long? ParentId { get; set; }
        public string ParentTitle { get; set; }
        public List<PermissionsViewModel> ParentList { get; set; }
        public List<PermissionsViewModel> OperationsList { get; set; }
        public List<PermissionTypesDto> PermissionTypesList { get; set; }
        public bool Status { get; set; }
    }

Method to retrieve data:

public Dictionary<long?,List<PermissionsViewModel>> GetPermissionsByModule()
        {
            var ItemValue = (_ipermissionTypes.Expose().FirstOrDefault(x => x.Title == "Operation").Id);

            var permissionbymodule = _ntumcontext.Tbl_Permissions
                .Where(x => x.Status == true && x.TypeId == ItemValue)
                .Select(x => new PermissionsViewModel
                {
                    ID = x.ID,
                    Title = x.Title,
                    Status = x.Status,
                    ParentId = x.ParentId,
                    ParentTitle=x.permission.Title,
                    TypeId=x.TypeId,
                }).AsEnumerable().GroupBy(x => x.ParentId).ToList();

            return permissionbymodule.ToDictionary(k => k.Key, v => v.ToList());
        }

And the Method to get on razor page (View):

public List<SelectListItem> Permissions = new List<SelectListItem>();
public List<SelectListItem> GetPermissionsByModule()
            {
                var AllPermissions = _ipermissionsApplication.GetPermissionsByModule();
                foreach (var (key, value) in AllPermissions)
                {
    
                    var parentTitle = _ipermissionsApplication.GetDetails(key).Title; //get group title from key
                    var group = new SelectListGroup() { Name = parentTitle };
                    foreach (var per in value)
                    {
                        var item = new SelectListItem(per.Title, per.ID.ToString())
                        {
                            Group = group
                        };
                        Permissions.Add(item);
                    }
    
                }
                return Permissions;
            }

And in cshtml :

<select asp-for="RoleVM.SelectedPermissions" asp-items="Model.Permissions">

At present with the above codes, the problem is that do not show the second and third and ... , only show the first group name, but show all child items of all groups.


Solution

  • Firstly,you need to check the value of AllPermissions,maybe the parentID is not exists,and then you need to check var parentTitle = _ipermissionsApplication.GetDetails(key).Title;maybe the second and third and.. parentTitle is null.

    Here is a working demo(I use fake data):

    public class TestPermissionsModel : PageModel
        {
            public List<SelectListItem> Permissions = new List<SelectListItem>();
            
            public void GetPermissionsByModule()
            {
                var AllPermissions = new Dictionary<long, List<PermissionsViewModel>>()
                {
                    {1,new List<PermissionsViewModel>{ new PermissionsViewModel{ ID=11,Title="title11", ParentId=1}, new PermissionsViewModel { ID = 12, Title = "title12", ParentId = 1 } } },
                    {2,new List<PermissionsViewModel>{ new PermissionsViewModel{ ID=21,Title="title21", ParentId=2}, new PermissionsViewModel { ID = 22, Title = "title22", ParentId = 2 } } },
                    {3,new List<PermissionsViewModel>{ new PermissionsViewModel{ ID=31,Title="title31", ParentId=3}, new PermissionsViewModel { ID = 32, Title = "title32", ParentId = 3 } } }
    
    
                };
                foreach (var (key, value) in AllPermissions)
                {
    
                    var parentTitle = "parentTitle" + key; //get group title from key
                    var group = new SelectListGroup() { Name = parentTitle };
                    foreach (var per in value)
                    {
                        var item = new SelectListItem(per.Title, per.ID.ToString())
                        {
                            Group = group
                        };
                        Permissions.Add(item);
                    }
    
                }
            }
            public void OnGet()
            {
                GetPermissionsByModule();
    
            }
        }
    

    View:

    <select  asp-items="Model.Permissions"></select>
    

    result:

    enter image description here