0

我有一个数据网格来显示员工的具体信息,但我需要设置一个逻辑,如果 Role=Manager 则显示所有员工,如果 Role=Employee 仅显示 1 个属于他的数据。下面是我显示员工的代码。我设法向所有员工展示。需要帮助为员工设置过滤器。

看法

@model IEnumerable<SealManagementPortal_3._0.Models.VOC_CUSTODIAN>
@{
    ViewBag.Title = "List of Custodians";
}
<h2>Index</h2>
<p> 
   @if (User.IsInRole("Manager"))
{
    @Html.ActionLink("Create New", "Create")
}
</p>
<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery("#list2").jqGrid({
            url: '@Url.Action("GridData", "Custodian")',
            datatype: 'json',
            mtype: 'GET',
            colNames: ['Agent ID', 'Branch', 'Unique ID', 'Custodian Name', /*'NRIC No', 'E-Mail', 'Contact No', 'Mobile No',*/'Role', 'Details', 'Edit', 'Delete'],
            colModel: [
                { name: 'Agent ID', index: '', width: 10, align: 'left' },
                { name: 'Branch', index: '', width: 10, align: 'left' },
                { name: 'Unique ID', index: '', width: 10, align: 'left' },
                { name: 'Custodian Name', index: '', width: 10, align: 'left' },          
                {name: 'Role', index: '', width: 10, align: 'left' },
                { name: 'Details', index: '', width: 5, align: 'left' }
                , { name: 'Edit', index: '', width: 5, align: 'left' }
                ,{ name: 'Delete', index: '', width: 5, align: 'left' }
                ],    
            pager: jQuery('#pager2'),
            rowNum: 10,            
            sortname: 'Id',
            sortorder: "desc",
            viewrecords: true,
            autowidth: true,            
            caption: 'Custodians List'
        });

    }); 
</script>

@using (Html.BeginForm())
{
    <table id="list2" class="scroll" cellpadding="0" cellspacing="0"></table>
    <div id="pager2" class="scroll" style="text-align:center;"></div>
}

控制器

     public ActionResult GridData (string sidx, string sord, int page, int rows)
            {             
                int pageIndex = Convert.ToInt32(page) - 1;
                int pageSize = rows;
                int totalRecords = db.VOC_CUSTODIAN.Count();
                int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);   
                var jsonData = new
                               {
                                   total = totalPages,
                                   page = page,
                                   records = totalRecords,
                                   rows = (from custo in db.VOC_CUSTODIAN.AsEnumerable()
                                          select new
                                                 {
                                                     id = custo.Idx,
                                                     cell = new string []
                                                            {
                                                                custo.StaffId, 
                                                                custo.Branch,
                                                                custo.UniqueId,
                                                                custo.Name,
                                                                custo.Role,
                                                                ("<a href='"+ @Url.Action("Details", "Custodian") +"/"+ custo.Idx+ "'>DETAILS</a>"),
                                                                ("<a href='"+ @Url.Action("Edit", "Custodian") +"/"+ custo.Idx+ "'>EDIT</a>"),
                                                                ("<a href='"+ @Url.Action("Delete", "Custodian") +"/"+ custo.Idx+ "'>DELETE</a>")
}
                                                 }).ToArray()
                               };
                return Json(jsonData, JsonRequestBehavior.AllowGet);
            }
4

1 回答 1

0

你的视图不应该处理逻辑,你的控制器动作应该。你应该可以用Roles.InUserInRole做这样的事情:

if (!Roles.IsUserInRole(User.Identity.Name, "Managers"))
{
  jsonData.total = 1;
  jsonData.page = 1;
  jsonData.records = 1;
  jsonData.rows = jsonData.rows.Where(x => x.id = currentUserId).ToArray() 
}

return Json(jsonData, JsonRequestBehavior.AllowGet);
于 2012-06-18T08:32:24.097 回答