AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-22874144

thecodeiackiller's questions

Martin Hope
thecodeiackiller
Asked: 2024-10-21 06:26:05 +0800 CST

如何维护 Blazor 表单中多个级联下拉菜单的单独状态?

  • 6

链接到 GitHub 以快速访问最小可重现示例(下面也提供了代码): https: //github.com/thecodeiackiller/cascadingSelectListProblem

  • #1)我有一份锻炼清单。
  • #2)我有 2 个枚举 (WorkoutType 和 ExerciseType),可以过滤锻炼列表。
  • #3)我有一个 EditForm,它允许用户: 选择 WorkoutType(LowerBody 或 UpperBody),它会过滤锻炼列表,并 选择 ExerciseType(Main、Secondary、Accessory),它会进一步过滤列表。

注意:对于那些不熟悉健身领域的人来说,在给定的锻炼中,您通常有一种锻炼类型(腿部锻炼、上身锻炼等),并且在该锻炼中,您有运动类型(主要/较重的运动、次要运动和辅助运动(锻炼小肌肉)。

预期按时间顺序排列的用户流量:

    1. 选择了 WorkoutType 并过滤了练习列表(我相信我的表格已经成功做到了这一点)。
    1. 对于 EditForm 中的第一行,用户选择一个 ExerciseType(主要、次要、附件)并且列表再次被过滤。
    1. 用户从现在经过两次筛选的列表中选择一项练习的名称。仍在第一行。
    1. 用户导航至第二行。
    1. 用户选择锻炼类型
    1. 用户从列表中选择新的练习名称(应根据第二行所选的练习类型进行过滤)。

问题:第二行的练习名称列表没有更新。练习名称列表与第一个练习相同,尽管练习类型不同,这应该会改变显示的练习。

我当前实现的AddWorkout.razor组件:

@using stateManagementMinimumReproducibleExample.Repositories
@using stateManagementMinimumReproducibleExample.Models
@page "/addworkout"
@rendermode InteractiveServer
@inject UserExerciseRepository userExerciseRepository

<EditForm Model="userExercise">
    <label for="workoutType"> Workout Type: </label>
    <InputSelect id="workoutType" @bind-Value="userExercise.WorkoutType" @bind-Value:after="GetExercisesByWorkoutType">
        @foreach (var type in Enum.GetValues(typeof(WorkoutType)))
        {
            <option value="@type">@type</option>
        }
    </InputSelect>

    @for (int i = 0; i < numberOfExercisesInWorkout; i++)
    {
        var j = i;
        if (j >= exerciseListForSelectedDay.Count)
        {
            exerciseListForSelectedDay.Add(new UserExercise());
        }

        <div>
            <label for="exerciseType"> Exercise Type: </label>
            <InputSelect id="exerciseType" @bind-Value="exerciseListForSelectedDay[j].ExerciseType" @bind-Value:after="GetExercisesByExerciseType">
                @foreach (var type in Enum.GetValues(typeof(ExerciseType)))
                {
                    <option value="@type">@type</option>
                }
            </InputSelect>

            <label for="exerciseName"> Exercise: </label>
            <InputSelect id="exerciseName" @bind-Value="exerciseListForSelectedDay[j].ExerciseId">
                @foreach (var exercise in listOfExerciseByExerciseType)
                {
                    <option value="@exercise.Id">@exercise.Name</option>
                }
            </InputSelect>
        </div>
    }
</EditForm>


@code {
    private int numberOfExercisesInWorkout = 2;

    private UserExercise userExercise = new();

    private List<UserExercise> exerciseListForSelectedDay = new();

    public List<Exercise> listOfExercisesByWorkoutType = new();

    public List<Exercise> listOfExerciseByExerciseType = new();

    public async Task GetExercisesByWorkoutType()
    {
            listOfExercisesByWorkoutType = await   userExerciseRepository.GetExercisesBasedOnWorkoutType(userExercise.WorkoutType);
            StateHasChanged();
    }

    public void GetExercisesByExerciseType()
    {
            listOfExerciseByExerciseType = userExerciseRepository.GetExercisesFromWorkoutListBasedOnExerciseType(listOfExercisesByWorkoutType, userExercise.ExerciseType);;
    }
}

我考虑过创建一个字典来存储锻炼中每项练习的练习列表的可能性。此外,我还考虑将 List 作为属性存储在我的 UserExercise 类中,然后将过滤后的练习列表添加到该属性中。我不确定我应该走哪条路,或者是否存在任何潜在的特定于 Blazor 的状态管理替代方案。

上面的代码应该能给你足够的信息来诊断问题。如果你需要直观的了解,以下是在分叉和克隆 GitHub 存储库后快速重现问题的步骤:

  • 启动应用程序
  • 导航至 /addworkout 端点
  • 选择锻炼类型(整个表格)
  • 选择锻炼类型(第一行)
  • 选择一项练习(第一行)
  • 选择锻炼类型(第二行)
  • 选择一项练习(第二行)

此时,您将看到,由于选择了与第一行不同的 ExerciseType,因此第二行的筛选列表尚未更新。我如何才能让第二行(及以后)的适当练习列表出现?谢谢。

blazor
  • 1 个回答
  • 33 Views
Martin Hope
thecodeiackiller
Asked: 2024-10-18 06:09:30 +0800 CST

Blazor InputSelect:无法使用 @bind-Value 将枚举值绑定到枚举类型

  • 5

我正在使用 Blazor Web 应用。我有一个枚举。它有三个值:UpperBody、LowerBody、FullBody。我的目标是让用户选择其中一个值。之后,程序将获取所选值(UpperBody、LowerBody、FullBody)并根据该值从数据库中筛选练习。这需要所选枚举值成功绑定到我的组件中的变量。我还没有找到这样做的方法。正如您将在我的代码中看到的,我还有一个方法,用于将所选枚举值打印到控制台。此外(正如您将看到的),我试图在 HTML 中打印枚举的值。这是为了调试,以便我可以“看到”我的更新值。

枚举:

namespace dataBindingProblem
{
    public enum WorkoutType
    {
        UpperBody, LowerBody, FullBody
    }
}

组件:

注意:此代码块缺少 EditForm 标签。因此,导航到 /selectworkout 端点时,您将遇到异常,指出需要使用 EditContext 或 Model。我不确定如何继续,因为我没有直接处理模型(尽管我确实尝试引入一个模型,如下所述)。

@page "/selectworkout"

<h3>Select Workout Type (From Enum Values) </h3>


    <label for="workoutselection"> Select Workout Type </label>
    <InputSelect id="workoutselection" @bind-Value="@selectedWorkoutType" @bind-Value:after="workoutTypeChanged">
        <option value=""> Select Workout Type </option>
        @foreach (var type in Enum.GetValues(typeof(WorkoutType)))
        {
            <option value="@type">@type</option>
        }
    </InputSelect>


<p>Selected Workout Type: @selectedWorkoutType</p>

@code {
    private WorkoutType selectedWorkoutType;

    public void workoutTypeChanged()
    {
        Console.WriteLine($"Workout Type Changed: {selectedWorkoutType}");
    }
}

我尝试过的事情

  • 我曾尝试重构我的组件以引入 UserExercise.cs 模型(如上所述),该模型具有属性 public WorkoutType WorkoutType {get; set;}。在此尝试中,我还将 EditContext 引入了 EditForm 标签。此尝试的灵感来自此。

  • 我尝试在 InputSelect 标签中使用 ValueChanged 参数。经过进一步检查,我将其替换为 bind-Value:after,灵感来自jeroba 的 2023 年解决方案。

  • 我曾尝试离开电脑,去 Qdoba 吃一碗墨西哥卷饼。

除了解决问题之外,如果以后有任何调试此类问题的建议,请告诉我。归根结底,我只想让 bind 工作。我考虑过重构应用程序并删除所有枚举,并为下拉列表引入一个内存列表,并最终过滤数据库,但我不认为枚举(及其相关模型)导致了问题。谢谢。

razor
  • 1 个回答
  • 27 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve