Search code examples
c#asp.net-mvc-4razordotnetnuke

Display HTML if user has role on Dotnetnuke using Razor


I'm using the Dotnetnuke CMS and am trying to modify a display template that is written in Razor.

I have a div i'd like to hide/display based on a user's role. I'm fairly new to Razor and C# and am having trouble getting this to work.

I've read up on DNN's UserController but am not sure this is for Razor or the ACSX asp.net template engine or CSHTML files?

My code is the following:

Dim userInfo = UserController.GetCurrentUserInfo()

if (userInfo.IsInRole("Administrators")) {
<div style="background-color:red;">IS ADMIN</div>
}
else if (userInfo.IsInRole("Dealers")) {
<div style="background-color:blue;">IS DEALER</div>
}
End If

Solution

  • I'm assuming you are using the Module Creator to build a quick module using the Razor syntax.

    Your code example is a mix of C# and VB syntax. Here is the code in C#.

    @using DotNetNuke.Common;
    @using DotNetNuke.Entities.Users;
    
    @{
        var userInfo = UserController.Instance.GetCurrentUserInfo();
    }
    
    @if (userInfo.IsInRole("Administrators")) {
        <div style="background-color:red;">IS ADMIN</div>
    }
    else if (userInfo.IsInRole("Dealers")) {
        <div style="background-color:blue;">IS DEALER</div>
    }