Home > Enterprise >  Getting two lists of data and need to display data in view for multiple rows
Getting two lists of data and need to display data in view for multiple rows

Time:03-15

I am getting two lists in my function.

In first list I am getting list of all users for particular operation it's code is as per below : Here in result I am getting full list of all users.

using (API = new APIHelper(APIURL, token))
{
   result = APIManager.GetUserListperOperation(Id.Value);
}

Now I need to get role name for particular user based on User id, now one user can have multiple roles so in few cases I am getting multiple role records for one user.

Like user A has role A as well as role B etc..

For that below is code :

foreach(var userDetails in result)
{
   role = APIManager.GetRoleperUser(userDetails.Id);
}

So I got two lists one is for users and other is for roles.

Now in view file I need to display list of users and also if user has more then one role then same user will display multiple times for each role,

Means user's other details will be displayed same like first name, last name but role name will be displayed different for each row. Like below image.

I have tried below code :

@foreach (var item in result)
{
   foreach(var userDetails in role)
   {
      <tr role="row" >
         <td style="word-break:break-all">
             @Html.DisplayFor(item.UserName)
         </td>
         <td>
             @Html.DisplayFor(userDetails.RoleName)
         </td>
      </tr>
   }
}

Any idea how to do this

CodePudding user response:

So Like Is said in my comment. You could either create the 'list to loop' before you create your table and then loop through it or create it in the controller and then loop through it (Which I think would be the easiest to do).

foreach (var userDetails in role)
{
   foreach(var item in result.Where(x => x.userId == userDetails.//Something?))
   {
       listX.Add(
           Email = item.Email,
           FirstName = item.FirstName,
           LastName = item.LastName,
           IsEnable = item.IsEnable,
           RoleStatus = item.RoleStatus,
           RoleDisplayName = userDetails.RoleDisplayName
       );
        
   }
}

listX = listX.OrderBy(x => x.Email).ThenBy(x => x.RoleDisplayName);

Then in the view just loop through the list:

 foreach(var i in listX)
 {
   <tr role="row" >
     <td style="word-break:break-all">
         @Html.DisplayFor(item.Email)
     </td>
     //etc
  </tr>
 }

CodePudding user response:

I have make it working using following way.

I have created new class and created new dynamic list.

UserGridDataList.Add(new UserGridData
                                {
                                    UserName = userDetails.UserName,
                                    FirstName = userDetails.FirstName,
                                    LastName = userDetails.LastName,
                                    IsEnabled = userDetails.IsEnabled,
                                    RoleName = zClientRoleModel.DisplayName,
                                    IsEnabledRole = zClientRoleModel.IsEnabled
                                });
  • Related