The problem is that I tried different ways to introduce authorization for a special tag from my navy bar. In navy bar I'm using model UserData, which import data from DB.

I have tried this code, but I have the error "Cannot convert lambda expression to type 'bool' because it is not a delegate type' if (u=>u.URole==“admin”) [on hold]"

@if (u => u.URole == "admin")
                {
                    <li class="nav-item">
                        <a class="nav-link" href="@Url.Action("Contact", "Home")">Contact</a>
                    </li>
                }

I have tried also, but I have the error "Operator '==' cannot be applied to operands of type 'URole' and 'string''"

@if (Model.URole == "admin")
                {
                    <li class="nav-item">
                        <a class="nav-link" href="@Url.Action("Contact", "Home")">Contact</a>
                    </li>
                }

I have this class where I get and set Username and Role from Microsoft SQL, DB.

public class UserData
{
    public string Username { get; set; }
    public URole URole { get; set; }
    public List<string> Products { get; set; }
    public string SingleProduct { get; set; }
}

And class URole which contain my roles

public enum URole
{
    User,
    Moderator,
    Admin
}

1 Answers

0
Louis Matthijssen On

You are comparing your enum URole to a string, which can't be done because they're different types and don't have an implicit conversion. You have to convert either side.

The best way would be to use the enum for your check as well, for example:

@if (Model.URole == URole.Admin)
{
    // Your HTML
}