c# - WebGrid MVC 5 – 显示基于身份验证和授权的操作按钮

标签 c# asp.net-mvc-5 webgrid

在WebGrid 的最后一列有3 个按钮“创建、编辑、删除”。如标题所示,如果用户经过身份验证和授权,我只想显示“编辑”和“删除”按钮。 enter image description here

如果链接不在 WebGrid 中,例如“添加”按钮,我可以这样做:

<div >
     @if (Request.IsAuthenticated && (User.IsInRole("Admin") || User.IsInRole("canEdit")))
     {
         <a class="btn btn-success" href="@Url.Action("Create" )" id="btnCreate"><i class='glyphicon glyphicon-plus'></i> </a>
     }
</div>

在网格中,我以这种方式格式化了最后一列:

grid.Column(header: "Action", canSort: false, style: "action  col-lg-2",
            format: (item) => new HtmlString("<a href=" + @Url.Action("Details", new { id = item.id }) + " title='Detail' ><i class='glyphicon glyphicon-search'> </i><span class='sr-only'>Detail</span> </a> " 
                + " <a href=" + @Url.Action("Edit", new { id = item.id }) + "><i class='glyphicon glyphicon-edit'></i><span class='sr-only'>Edit</span> </a> " 
                + " <a href=" + @Url.Action("Delete", new { id = item.id }) + "><i class='glyphicon glyphicon-trash'> </i> </a> ") 
              )

而且效果很好。但我无法弄清楚如何将 if 语句放入 lambda 表达式中。我该怎么做?

代码如下:

var grid = new WebGrid(canPage: true, rowsPerPage: Model.PageSize, canSort: true, ajaxUpdateContainerId: "grid");
grid.Bind(Model.Content, rowCount: Model.TotalRecords, autoSortAndPage: false);
@grid.GetHtml(htmlAttributes: new { id = "grid" },  // id for ajaxUpdateContainerId parameter
fillEmptyRows: false,
tableStyle: "table table-bordered table-hover",
mode: WebGridPagerModes.All,
columns: grid.Columns(
         grid.Column("Id", "ID", style: "col-lg-1"),
         grid.Column("ProductName", "Name", style: "col-lg-3"),
         grid.Column("Description", "Description", style: "col-lg-5"),
         grid.Column(header: "Action", canSort: false, style: "action  col-lg-2",
         format: (item) => new HtmlString("<a href=" + @Url.Action("Details", new { id = item.id }) + " title='Detail' ><i class='glyphicon glyphicon-search'> </i><span class='sr-only'>Detail</span> </a> " 
             + " <a href=" + @Url.Action("Edit", new { id = item.id }) + "><i class='glyphicon glyphicon-edit'></i><span class='sr-only'>Edit</span> </a> " 
             + " <a href=" + @Url.Action("Delete", new { id = item.id }) + "><i class='glyphicon glyphicon-trash'> </i> </a> ")                   )
             )
         );

最佳答案

您可以通过以下方式进行授权。

@if ( User.Identity.IsAuthenticated ){
    if ( User.IsInRole("Admin") ){
        + " <a href=" + @Url.Action("Edit", new { id = item.id }) + "><i class='glyphicon glyphicon-edit'></i><span class='sr-only'>Edit</span> </a> " 
                + " <a href=" + @Url.Action("Delete", new { id = item.id }) + "><i class='glyphicon glyphicon-trash'> </i> </a>        
    }
}

并记得将 [Authorize] 属性添加到您的 Admin 操作方法:

[Authorize(Roles="Admin")]
public ActionResult Edit()
{
    // ...
    return View();
}

[Authorize(Roles="Admin")]
public ActionResult Delete()
{
    // ...
    return View();
}

关于c# - WebGrid MVC 5 – 显示基于身份验证和授权的操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31954101/

相关文章:

ASP.NET MVC5 通过单击 Html.ActionLink 更改语言/文化

c# - 如何在不使用 Entity Framework 的情况下使用 ASP NET MVC5 显示 SQL Server 中的表?

asp.net-mvc-3 - MVC 3 WebGrid - 是否可以进行内联编辑?

asp.net-mvc-3 - 格式化 WebGrid 列以包含两条数据

asp.net-mvc - 在Webgrid中将 bool 值显示为复选框

c# - Linq方法获取列表中元素和下一个元素之间的分段差异

c# - MVC 部分页面刷新

asp.net-mvc-4 - 如何从 SimpleMembership 迁移到 ASP.NET.Identity

c# - 我的对话框返回值不起作用

c# - 如何在 Automapper 中从源代码制作部分 map