Reputation: 117
I'm building a help desk application using C# MVC. I have a 'Ticket' view. In my Ticket model I have a 'RaisedBy' property. I have been able to apply some logic to show a logged in user their tickets. The next step I'd like to show all Tickets to a user with the role 'admin'
I've created a foreach loop to return all items and pop them into a table. Here's what I've tried so inside my view:
@foreach (var item in Model) {
if (item.RaisedBy == User.Identity.Name) {
if(item.RaisedBy == User.IsInRole ="Admin")
This gives me the error:
Operator '==' cannot be applied to operands of type 'string' and 'method-group'
I've also tried:
@foreach (var item in Model) {
{
if (item.RaisedBy = User.IsInRole("Admin") {
Which gives:
Cannot implicitly convert type 'bool' to 'string'
I'm not very experienced with C# so any pointers or suggestions would be really appreciated.
Upvotes: 1
Views: 485
Reputation: 16701
User.IsInRole
acts on the current logged in user and it is a method. It returns a bool
.
You're using the wrong syntax here: if (item.RaisedBy = User.IsInRole("Admin"))
. It should be if (User.IsInRole("Admin"))
.
Once you've confirmed the item.RaisedBy
matches the current user then you just need to check if the current user has admin role.
@foreach (var item in Model)
{
// if item.RaisedBy is current user
if (item.RaisedBy == User.Identity.Name)
{
// if current user is in "admin" role
if(User.IsInRole("Admin"))
{
}
}
}
Upvotes: 1
Reputation: 3177
You're not using equality comparison
==
. By using=
you're assigning the righter variable to the left.
Your loop should be:
@foreach (var item in Model) {
if (item.RaisedBy == User.Identity.Name) {
if (User.IsInRole("Admin"))
// Other code..
Upvotes: 1