Conditioner to show a button

0

Good afternoon, I hope I can get answers here. I'm working on ASP.net Razor 5 and Entity Framework I have a list in which I have a button called ENABLE. I need that button to enable only show if a certain user is logged in. Can I do it directly in the html? A bit of the code:

<div class="container panel-table">
    @Html.Grid(Model).Named("usuariosGrid").Columns(
         columns =>
         {
         columns.Add(c => c.Nombre).Titled("Nombre");
         columns.Add(c => c.UserName).Titled("Usuario");
         columns.Add(c => c.Iniciales).Titled("Iniciciales");
         columns.Add(c => c.Cargo).Titled("Cargo");
         columns.Add(c => c.Email).Titled("Email");
         columns.Add(c => c.FechaText).Titled("Fecha de Nacimiento");
         columns.Add(c => c.HabilitadoText).Titled("Habilitado");
         columns.Add(c => c.RoleName).Titled("Rol");
         columns.Add(c => c.Sucursal).Titled("Sucursal");
         columns.Add().Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(c => @<a href="@Url.Action("UserEdit", "account", new { Id = @c.Id })" class="btn btn-warning btn-xs">Editar</a>);

         columns.Add().Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(c => @<a href="#myModal" class="btn btn-warning btn-xs habilitar" id="Habilitar" data-id="@c.Id">Habilitar</a>);

         columns.Add().Encoded(false).Sanitized(false).SetWidth(0).RenderValueAs(c =>
        @<a href="#myModal" class="btn btn-danger btn-xs btnEliminar" data-id="@c.Id">Eliminar</a>);

         }
    ).WithPaging(10).Sortable(true).WithMultipleFilters().Selectable(true).SetLanguage("es").Filterable(true)
</div>
    
asked by Maximiliano Cesán 26.10.2017 в 19:30
source

2 answers

0

Here you show the button depending on the name of the logged in user:

@if (User.Identity.GetUserName() == "Maximiliano")
    {
        <button class="btn btn-success">Habilitar</button>
    }

and Here depending on the user's Roll.

  @if (User.IsInRole("Admin"))
    {
        <button class="btn btn-success">Habilitar</button>
    }

I hope it helps you

    
answered by 12.12.2017 в 16:36
0

UPDATE: To work perfectly next to .Add () we have to render the value with RenderValueAs () and within it open an @div where we can use the @if otherwise it will not work. SOLUTION:

columns.Add().Encoded(false).Sanitized(false).SetWidth(30).RenderValueAs(c => @<div>
        @if (c.HabilitadoText == "Habilitado")
        {
            <i class="fa fa-check" style="color:green;"></i>
        }
        else
        {
            <i class="fa fa-times" style="color:red;"></i>
        }
    </div>).Titled("Estado");
    
answered by 12.12.2017 в 16:51