如何过滤 IList<Models.HomeMenuOverview> HomeMenuOverviews { get;放; }
How can I filter an IList<Models.HomeMenuOverview> HomeMenuOverviews { get; set; }
该模型包含用户 ID,我在会话 ID (SessionIdId) 中有用户 ID (WorkColUserId),因此我可以过滤 IList,或者我可以生成一个已经减少的 IList 以减少主菜单,具体取决于权利上。感谢您的帮助。
====这是我的模型====
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace WorkCollaboration.Models
{
public class HomeMenuOverview
{
[Key]
public int MenuId { get; set; }
public string MenuText { get; set; }
public string AssignedFunction { get; set; }
public bool PermissionIndex { get; set; }
public bool PermissionCreate { get; set; }
public bool PermissionEdit { get; set; }
public bool PermissionDetail { get; set; }
public bool PermissionDelete { get; set; }
public int RoleId { get; set; }
public string RoleName { get; set; }
public int WorkColUserId { get; set; }
public string WorkColUserName { get; set; }
}
}
==== 这是我的 C# 代码 =====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using WorkCollaboration.Models;
namespace WorkCollaboration.Pages
{
public class IndexModel : PageModel
{
private readonly WorkCollaboration.Data.WorkCollaborationContext _context;
public IndexModel(WorkCollaboration.Data.WorkCollaborationContext context)
{
_context = context;
}
public IList<Models.HomeMenuOverview> HomeMenuOverviews { get; set; }
public async Task OnGetAsync()
{
string SessionKeyName = "_Name";
string SessionKeyId = "_Id";
string SessionKeyDate = "_Date";
var SessionIdDate = HttpContext.Session.GetString(SessionKeyDate);
var SessionIdId = HttpContext.Session.GetString(SessionKeyId);
var SessionIdName = HttpContext.Session.GetString(SessionKeyName);
HomeMenuOverviews = await _context.HomeMenuOverview.ToListAsync();
}
}
}
====这是我的页面=====
@page
@model IndexModel
@using System.Web
@using WorkCollaboration.Models
@{
ViewData["Title"] = "Home page";
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div class="text-center">
<h3 class="display-4">Welcome</h3>
</div>
<h1>Menu</h1>
<table class="table">
<thead>
<tr>
</tr>
</thead>
<tbody>
@foreach (var item in Model.HomeMenuOverviews)
{
<tr>
<td>
<a class="nav-link" asp-area="" asp-page=@item.AssignedFunction>@item.MenuText</a>
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
=====这里是未过滤数据的应用图片=====
!菜单图片1
如果你想用SessionId过滤HomeMenuOverview,你可以这样做:
HomeMenuOverviews = await _context.HomeMenuOverview.Where(h => h.WorkColUserId == SessionIdId).ToListAsync();
该模型包含用户 ID,我在会话 ID (SessionIdId) 中有用户 ID (WorkColUserId),因此我可以过滤 IList,或者我可以生成一个已经减少的 IList 以减少主菜单,具体取决于权利上。感谢您的帮助。
====这是我的模型====
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
namespace WorkCollaboration.Models
{
public class HomeMenuOverview
{
[Key]
public int MenuId { get; set; }
public string MenuText { get; set; }
public string AssignedFunction { get; set; }
public bool PermissionIndex { get; set; }
public bool PermissionCreate { get; set; }
public bool PermissionEdit { get; set; }
public bool PermissionDetail { get; set; }
public bool PermissionDelete { get; set; }
public int RoleId { get; set; }
public string RoleName { get; set; }
public int WorkColUserId { get; set; }
public string WorkColUserName { get; set; }
}
}
==== 这是我的 C# 代码 =====
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using WorkCollaboration.Models;
namespace WorkCollaboration.Pages
{
public class IndexModel : PageModel
{
private readonly WorkCollaboration.Data.WorkCollaborationContext _context;
public IndexModel(WorkCollaboration.Data.WorkCollaborationContext context)
{
_context = context;
}
public IList<Models.HomeMenuOverview> HomeMenuOverviews { get; set; }
public async Task OnGetAsync()
{
string SessionKeyName = "_Name";
string SessionKeyId = "_Id";
string SessionKeyDate = "_Date";
var SessionIdDate = HttpContext.Session.GetString(SessionKeyDate);
var SessionIdId = HttpContext.Session.GetString(SessionKeyId);
var SessionIdName = HttpContext.Session.GetString(SessionKeyName);
HomeMenuOverviews = await _context.HomeMenuOverview.ToListAsync();
}
}
}
====这是我的页面=====
@page
@model IndexModel
@using System.Web
@using WorkCollaboration.Models
@{
ViewData["Title"] = "Home page";
}
<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>
<body>
<div class="text-center">
<h3 class="display-4">Welcome</h3>
</div>
<h1>Menu</h1>
<table class="table">
<thead>
<tr>
</tr>
</thead>
<tbody>
@foreach (var item in Model.HomeMenuOverviews)
{
<tr>
<td>
<a class="nav-link" asp-area="" asp-page=@item.AssignedFunction>@item.MenuText</a>
</td>
</tr>
}
</tbody>
</table>
</body>
</html>
=====这里是未过滤数据的应用图片=====
!菜单图片1
如果你想用SessionId过滤HomeMenuOverview,你可以这样做:
HomeMenuOverviews = await _context.HomeMenuOverview.Where(h => h.WorkColUserId == SessionIdId).ToListAsync();