Partial view rendering by condition

I have several partial views that should be displayed when the user has a specific role.

Now I want to not do something like

<% if(user is in role){..here goes the html.. }%>

I would like to be able to do (at the top of ascx):

<% this.RenderOnlyForRoles(list of roles) %>

Now in BasePartialView I have a list of roles that populates when I call RenderOnlyForRoles.

The problem is that RenderOnlyForRoles is called after .. all the events that I can think of :), and I cannot stop the rendering of the control.

Any ideas on how to get what I want?

EDIT: Does anyone know if other views can support this?

+3
source share
2 answers

Use HTMLHelper

public static void RenderOnlyForRoles(this HtmlHelper html, List<string> roles))
{
    if (check if user in roles)
    {
        html.RenderPartial(yourview);
    }
}

Kindness,

Dan

+4

, . , .

, ; Null View, .

ASP.NET MVC 2 , , , .

0

Source: https://habr.com/ru/post/1724930/


All Articles