You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Asp Zero用户页面如何添加DropDownList并从Lookups表取值关联部门

Asp Zero 用户页面新增部门下拉框并实现权限控制方案

一、后端数据准备

  • 第一步:确保Lookups表中已经存储所有部门数据,LookupType字段设置为"Department"用来筛选部门类别的枚举数据
  • 第二步:在User实体类中新增外键字段关联部门ID,代码示例:
    public int? DepartmentId { get; set; }
    
    字段添加完成后执行EF Core迁移命令更新数据库:Add-Migration Add_DepartmentId_To_User,之后执行Update-Database
  • 第三步:在用户对应的CreateUserDto和UpdateUserDto中新增DepartmentId字段,保持和实体字段一致
  • 第四步:在UserAppService中新增获取部门下拉选项的应用服务方法:
    public async Task<List<ComboboxItemDto>> GetDepartmentOptions()
    {
        return await _lookupRepository
            .Where(l => l.LookupType == "Department")
            .Select(l => new ComboboxItemDto
            {
                Value = l.Id.ToString(),
                Text = l.Name
            }).ToListAsync();
    }
    
    注:ComboboxItemDto是Asp Zero内置的下拉选项传输类,可直接复用

二、前端页面新增下拉框控件

以MVC版本的Asp Zero为例:

  • 第一步:在用户创建/编辑页的.cshtml文件中新增下拉框组件:
    <div class="form-group">
        <label>@L("Department")</label>
        <select name="DepartmentId" id="DepartmentSelect" class="form-control">
        </select>
    </div>
    
  • 第二步:在页面对应的js文件中,页面加载时调用后端接口填充下拉选项:
    $(function(){
        // 加载部门选项
        _userService.getDepartmentOptions().done(function(result){
            result.forEach(function(item){
                $('#DepartmentSelect').append(`<option value="${item.value}">${item.text}</option>`);
            });
            // 编辑页回显已有部门数据
            if (editMode){
                $('#DepartmentSelect').val(user.departmentId);
            }
        });
    })
    
  • 第三步:修改表单提交逻辑,将选中的DepartmentId值传入后端创建/更新用户接口即可

三、文档权限控制实现

在归档文档的业务接口中添加权限过滤逻辑:

  • 提前定义主部门ID常量,或在系统配置表中存储主部门ID便于灵活修改
  • 文档查询接口添加部门过滤逻辑,代码示例:
    public async Task<List<ArchiveDocumentDto>> GetDocumentList()
    {
        var currentUserId = AbpSession.UserId.Value;
        var currentUser = await _userRepository.GetAsync(currentUserId);
        IQueryable<ArchiveDocument> query = _archiveDocumentRepository.GetAll();
        
        // 非主部门用户仅能查看本部门文档
        if (currentUser.DepartmentId != 主部门ID)
        {
            query = query.Where(d => d.DepartmentId == currentUser.DepartmentId);
        }
        
        return await query.ProjectTo<ArchiveDocumentDto>().ToListAsync();
    }
    
  • 编辑、新增接口同理,非主部门用户操作时校验文档所属部门是否和当前用户部门一致,不一致则抛出权限异常即可

内容的提问来源于stack exchange,提问作者Morgan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 12:51:03