Reputation: 1
Small Question,
I am having the following in my c# mvc project code:
div class="card-body">
@if (User.IsInRole("Secretaris"))
{
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.FirstName)</th>
</tr>
@foreach (var item in Model)
{
if (User.IsInRole("Secretaris"))
{
<tr>
<td>@Html.DisplayFor(modelitem => item.FirstName)</td>
</tr>
}
}
</table>
}
</div>
When I run the code everything works fine but when I go into the foreach it still gives all the names and not only the names that have the role "Secretaris". Hope someone can help me in what I am doing wrong.
Thanks in advance.
Roel Knippen
Upvotes: 0
Views: 860
Reputation: 1535
div class="card-body">
@if (User.IsInRole("Secretaris"))
{
<table class="table">
<tr>
<th>@Html.DisplayNameFor(model => model.FirstName)</th>
</tr>
@foreach (var item in Model)
{
if (item.role == "Secretaris")
{
<tr>
<td>@Html.DisplayFor(modelitem => item.FirstName)</td>
</tr>
}
}
</table>
}
</div>
Maybe something like this where you check if the item.role is secretaris?
Upvotes: 0
Reputation: 33738
Since your Model
has a .Firstname
property I assume your model represents a person, likely a user.
@User
represents the currently logged in user - NOT the user from your model.
You will need to include role information in your model if you want to work with users/permissions in your model. Something like
if (modelItem.Roles.Contains("Sec...")) { }
Upvotes: 1
Reputation: 3030
To show the table to begin with, you check to see if the User has the Secretaris role. So it either will or will not show, depending on that role.
However, then within the foreach loop you check to see if the user is in that role again, which is redundant because that code won't even run if it's not due to your enclosing if statement.
I'm assuming you want to run some kind of check on the "item" object that is an element of the Model.
Upvotes: 0