Blazor WASM 中的 MudSelect 问题

Posted

技术标签:

【中文标题】Blazor WASM 中的 MudSelect 问题【英文标题】:MudSelect Issue in Blazor WASM 【发布时间】:2021-04-22 11:53:17 【问题描述】:

我是 Blazor WASM 的新手,我正在开发一个使用 MudBlazor 的项目。但是,我对 MudSelect 有疑问。我想在 MudSelect 中显示部门名称。但是,我需要绑定DepartmentID。下面的代码部分工作,但当我想编辑个人时,只显示DepartmentID。它没有转换为部门名称,但是当我点击屏幕时,它变成了DepatmentName

我哪里做错了?提前致谢。

<MudSelect @bind-Value="personal.DepartmentID" Label="Select Department"  Variant="Variant.Outlined"  OffsetY="true">

    @foreach (var department in departments)
          <MudSelectItem  Value="@(department.DepartmentID)">@item.DepartmentName</MudSelectItem>
    
</MudSelect>       

@代码部分

@code
     [Parameter] public List<Department> departments  get; set;  = new List<Department>();

      protected async override Task OnInitializedAsync()
      
            departments = await departmentRepository.GetDepartment();
      

存储库

    public async Task<List<Department>> GetDepartment()
    
        var response = await httpService.Get<List<Department>>(url);
        
        if (!response.Success)
        
            throw new ApplicationException(await response.GetBody());
        

        return response.Response;   
    

控制器

   [HttpGet]
    public async Task<ActionResult<List<Department>>> Get() 
    
        return  await context.Departments.ToListAsync();
    

【问题讨论】:

你能分享你的@code部分吗? 您的代码显示显示@item.DepartmentName,但您的值引用@(department.DepartmentID)item 是什么?当然你应该显示@department.DepartmentName 【参考方案1】:

您可以直接使用Department 作为 MudSelect 的值。您需要做的就是确保 Department 类具有合适的相等性覆盖。我创建了一个工作演示,您可以在以下位置在线执行:https://try.mudblazor.com/snippet/cEwFFmbgRxLbvVQg

<MudSelect @bind-Value="_selectedDepartment" Label="Select Department"  Variant="Variant.Outlined"  OffsetY="true">

    @foreach (var department in departments)
          <MudSelectItem  Value="@department">@department.DepartmentName</MudSelectItem>
    
</MudSelect>
<br/><br/>
Selected Department: @_selectedDepartment?.DepartmentName

@code 
    Department _selectedDepartment;

    IEnumerable<Department> departments = new List<Department>()
    
        new Department() DepartmentID = 1, DepartmentName = "Biology",
        new Department() DepartmentID = 2, DepartmentName = "Physics",
        new Department() DepartmentID = 3, DepartmentName = "Chemistry",
        new Department() DepartmentID = 4, DepartmentName = "Mathematics",
        new Department() DepartmentID = 5, DepartmentName = "Law",
        new Department() DepartmentID = 6, DepartmentName = "Sociology"
    ;

    public class Department
    
        public int DepartmentID  get; set; 
        public string DepartmentName  get; set; 
        public override bool Equals(object o) 
            var other = o as Department;
            return other?.DepartmentID==DepartmentID;
        
        public override int GetHashCode() => DepartmentID.GetHashCode();        
        public override string ToString() 
            return DepartmentName;
        
    
 

【讨论】:

以上是关于Blazor WASM 中的 MudSelect 问题的主要内容,如果未能解决你的问题,请参考以下文章

具有 ReportViewer 导出功能的 Blazor (Wasm)

IdentityServerBuilderConfigurationExtension 中的 Blazor WASM 托管身份验证空引用异常

Blazor WASM 路由发布请求到 index.html

Blazor WASM 在组件之间传递值

blazor wasm 在 program.cs Main 中注入 IJSRuntime

Blazor_WASM之4:路由