Click here to Skip to main content
15,212,510 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi I have an application in which i have User , Roles and USer&Roles Tables My User table is just like below

USER (UserId,NAme)
Roles(RoleId,RoleName)
User&Roles(tableid,UserId,RoleId)
Although I have other columns as well but for simplicity i am taking only the required ones .

I have records in my User table , Roles table and User&roles table Like below
User Table(1,SAN)(2,JAH)(3,HOP)
Role TABLE(1,Admin)(2,Customer)(3,Super-ADmin)
User&Roles(1,1,1)(2,1,2)(3,2,2)(4,2,3)
As you can see My user id =1 is having two roles and user id 2 is having one role


I want to show the records on index view such like that as user id =1 is having two roles in the database table (user&Roles) i want to show one row but on role_name column i want comma seperated list of roles assigned to him rather than showing all rows with new role_name and all other information same .

What I have tried:

I have showed but it shows all rows like the link
<pre>@model IEnumerable<CMS_Monitoring.Models.UserRoles>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout2.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.User_Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Active)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Role_Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date_Assigned)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date_Created)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Date_Modified)
        </th>
        <th>Actions</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Registration.Username)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Active)
        </td>
     
            <td>
                @Html.DisplayFor(modelItem => item.Roles.Role_Name)
            </td>
        

        <td>
            @Html.DisplayFor(modelItem => item.Date_Assigned)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date_Created)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Date_Modified)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}

</table>
Posted
Updated 18-Nov-19 9:18am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Change your view so that it accepts a list of users instead of a list of user roles:
@model IEnumerable<CMS_Monitoring.Models.User>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout2.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<table class="table">
<thead>
    <tr>
        <th scope="col">Name</th>
        <th scope="col">Roles</th>
        <th scope="col">Actions</th>
    </tr>
</thead>
<tbody>
@foreach (var item in Model) 
{
    <tr>
        <th scope="row">
            @Html.DisplayFor(m => m.Username)
        </th>
        <td>
            @string.Join(", ", item.Roles.Select(ur => ur.Role.Name))
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Id })
        </td>
    </tr>
}
</tbody>
</table>
If you're using Entity Framework, don't forget to .Include(...) the roles when you load the collection of users.
var users = Context.Users.AsNoTracking().Include(u => u.Roles.Select(ur => ur.Role)).ToList();
return View(users);
   
Comments
Malikdanish 18-Nov-19 23:19pm
   
My Model is UserRoles not Users Table , and in my User table there is not any relationship with UserRoles Table , but the UserRoles table have the foriegnkey of UserId in it .so which model class i need to use with my view
Richard Deeming 19-Nov-19 7:06am
   
If you want to display a list of users in your view, then your model needs to be the list of users, not the list of user roles.

If you want to include the role names, then you'll need suitable navigation properties in place.
Malikdanish 19-Nov-19 0:02am
   
@string.Join(", ", item.Roles.Select(ur => ur.Role.Name)) This line says that role does not contain a definition of Select ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100