Optgroup for dropdown in MVC 4

In my MVC 4 application, I searched the optgroup for dropdownlist and found this very helpful.

I followed the steps and achieved this, but I am currently facing some problems. My controller code looks like this:

Model.ddlProject = db.Projects.OrderBy(t => t.Type)
                    .Select(t => new GroupedSelectListItem
                     {
                       GroupName = t.Type,
                       Text = t.Name,
                       Value = SqlFunctions.StringConvert((decimal)t.Id)
                      });

      

The grouping is done here, but I want it in a different way. With this I can associate a dropdown menu like below:

enter image description here

Now let me tell you about my table structure. I have one table with the following structure:

enter image description here

Here I have one hierarchy such that Client -> Client -> Projects. Thus, I need to group Clients in such a way that all projects are listed under Clients. I need something like this:

enter image description here

Hence, I am stuck with a request here, a request to achieve this. I need to show all client names in group name and for clients I need to list all relevant projects. I think I understand what the requirement is. Because it's stuck here so any help would be great. Thanks in advance.

+3


source to share


1 answer


As Stephen Muecke said in the comment section, projects and clients are two different things. You should have a Client table and a separate Project table containing a foreign key column in the Client table. So the solution below matches the existing table structure.

Fictitious scheme

DECLARE @clientprj TABLE ([id] int identity(1,1), [name] varchar(30), Desc1 varchar(30), ParentId int, pType varchar(30));

INSERT @clientprj ([name], Desc1, ParentId, pType) VALUES ('client1', '', 0, 'clt'), ('prj1', '', 1, 'prj'), ('prj2', '', 1, 'prj'), ('client2', '', 0, 'clt'), ('prj n', '', 4, 'prj')

      

and here is the request

SELECT GroupName, GroupKey, ProjName, ProjId
FROM 
(
    (
        SELECT NAME AS GroupName
            ,Id AS GroupKey
        FROM @clientprj m
        WHERE ParentId = 0
        ) m 
    FULL OUTER JOIN 
    (
        SELECT NAME AS ProjName
            ,Id AS ProjId
            ,ParentId
        FROM @clientprj
    ) 
    t ON m.GroupKey = t.ParentId
)
WHERE ParentId <> 0

      



Which return the following output.

GroupName   GroupKey    ProjName    ProjId
client1       1           prj1           2
client1       1           prj2           3
client2       4           prj n          5

      

and your controller method that calls this request like this:

Model.ddlProject = db.NewMethod
                    .Select(t => new GroupedSelectListItem
                     {
                       GroupName = t.GroupName,
                       GroupKey = t.GroupKey.ToString(),
                       Text = t.ProjName,
                       Value = t.ProjId.ToString()
                      });

      

and then bind your dropdown. Good luck...

+1


source







All Articles