实现目标的一种简单方法是创建一个设置表,在其中按组指定每个字段的可见性。
首先你需要制作一个这样的组(品牌)表:
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
}
那么您将需要一个用于可见性设置的表格:
public class TableVisibilitySettings
{
public int Id { get; set; }
public int GroupId { get; set; }
public virtual Group Group { get; set; }
public bool ContructionYear { get; set; }
public bool Power { get; set; }
public bool IsConvertible { get; set; }
}
然后您将需要您的表格和视图模型:
public class Table
{
public int Id { get; set; }
public int GroupId { get; set; }
public virtual Group Grup { get; set; }
public string Color { get; set; }
public int? ConstructionYear { get; set; }
public string Power { get; set; }
public bool? IsConvertible { get; set; }
public IEnumerable<TableVm> GetTableByGroupType(int groupId, ApplicationDbContext context)
{
var table = context.Tables.ToList();
var visibility = context.TableVisibilitySettings.FirstOrDefault(x => x.GroupId == groupId);
return table.Select(x => new TableVm
{
Id = x.Id,
Brand= x.Grup.Name,
Color = x.Color,
ConstructionYear = visibility.ContructionYear == true ? x.ConstructionYear : null,
Power = visibility.Power == true ? x.Power : null,
IsConvertible = visibility.IsConvertible == true ? x.IsConvertible : null
}).ToList();
}
}
使用GetTableByGroupType 方法,您可以根据每个组的可见性设置检索数据库。
如果您愿意,可以使用角色而不是组。
编辑:
一种应用分页的方法可以是这样的:
public IEnumerable<TableVm> GetTableByGroupWithPag(int groupId, ApplicationDbContext context,int pageNumber, int rowsPerPage)
{
var table = context.Tables.Skip((pageNumber-1)*rowsPerPage).Take(rowsPerPage).ToList();
var visibility = context.TableVisibilitySettings.FirstOrDefault(x => x.GroupId == groupId);
return table.Select(x => new TableVm
{
Id = x.Id,
Group = x.Grup.Name,
Color = x.Color,
ConstructionYear = visibility.ContructionYear == true ? x.ConstructionYear : null,
Power = visibility.Power == true ? x.Power : null,
IsConvertible = visibility.IsConvertible == true ? x.IsConvertible : null
}).ToList();
}
首先,您需要从表格中获取要显示的行,而不是只需要应用可见性设置。
编辑:
根据您的应用程序设计和技能,有多种方法可以将组链接到用户。
最简单的方法是在ApplicationUser 和Group 之间设置one to one 或many to many 关系,如下所示:
public class ApplicationUser
{
...
public int GroupId {get;set;}
public virtual Group Group
}
并且在 Group 类中你需要添加:
public virtual ICollection<ApplicationUser> Users {get;set;}
另一种方法是为每个品牌创建角色,并根据您希望他读/写的品牌为每个用户分配一个或多个角色。
另一种方法是使用声明,您只需向每个用户添加代表 groupId 或 groupName 或品牌的声明。
希望这将帮助您选择一种将用户链接到组的方式。