如何在同一页面上使用Blazor添加多个下拉列表?

huangapple go评论99阅读模式
英文:

How to add multiple dropdown list on same page in Blazor

问题

我正在尝试在单个页面中多次插入相同的下拉列表。

我有一个项目 X

我有一个 ProcessAreas 列表 (a, b, c, d, e)

我有一个 Instructions 列表 (1, 2, 3, 4, 5, 6)

我需要创建一个表单,为 X 项目的每个 ProcessArea 关联一个单独的指令(非强制性的)

所以,例如,对于项目 X1,我想要保存到我的数据库
(a,1),(b,3),(e,6)

表示对于项目 X1,我有指令 1 用于区域 a,指令 3 用于区域 b,指令 6 用于区域 e。其他区域未使用。

为了做到这一点,我首先读取了先前插入数据库中的所有指令。

我对 ProcessAreas 进行 foreach 循环,并创建一个下拉列表。在这个下拉列表中,我放置了可用指令的列表,如下所示

@foreach (var processArea in _availableProcessAreas)
{
    var label = "Select instructions for " + processArea.ProcessAreaName;
    var x = _model.ProcessAreaInstructions.FirstOrDefault(x => x.ProcessAreaId == processArea.ProcessAreaId)?.InstructionId;

    <MudSelect T="int?" Label=@label id="@processArea.ProcessAreaId" @bind-Value="x" For="(() => x)">
        @foreach (var instruction in _instructions)
        {
            <MudSelectItem T="int?" Value="@instruction.Id">(@instruction.Name) @instruction.Description</MudSelectItem>
        }
    </MudSelect>
} 

我可以在页面上看到下拉列表,但无法选择与显示的值不同的值。

您可以在此链接中找到具有此奇怪行为的完整示例

https://try.mudblazor.com/snippet/QYGxuWFEflpORBVt

您能帮助我解决这个问题并将更新后的数据保存到数据库吗?

非常感谢您提供的任何帮助。

英文:

I'm trying to insert same dropdown list multiple times in a single page.

I have an item X

I have a list of ProcessAreas (a,b,c,d,e)

I have a list of Instructions (1,2,3,4,5,6)

I need to create a form to associate to the X item a single instruction for every ProcessArea (not mandatory)

So, for example, for the item X1 I want to save to my database
(a,1),(b,3),(e,6)

Indicating that for item X1 i have instructions 1 for area a, instructions 3 for area b and instruction 6 for area e. The other areas are not used.

To do this I read all instructions previously inserted in db.

I do a foreach on ProcessAreas and create a dropdown list. Inside this dropdown I put the list of available instructions, like this

@foreach (var processArea in _availableProcessAreas)
{
    var label = &quot;Select instructions for &quot; + processArea.ProcessAreaName;
    var x = _model.ProcessAreaInstructions.FirstOrDefault(x =&gt; x.ProcessAreaId == processArea.ProcessAreaId)?.InstructionId;

    &lt;MudSelect T=&quot;int?&quot; Label=@label id=&quot;@processArea.ProcessAreaId&quot; @bind-Value=&quot;x&quot; For=&quot;@(() =&gt; x)&quot;&gt;
        @foreach (var instruction in _instructions)
        {
            &lt;MudSelectItem T=&quot;int?&quot; Value=&quot;@instruction.Id&quot;&gt;(@instruction.Name) @instruction.Description&lt;/MudSelectItem&gt;
        }
    &lt;/MudSelect&gt;
} 

I can see the dropdowns on page, but I cannot select a value different from the one is displayed.

Here you can find a full sample with this strange behavior

https://try.mudblazor.com/snippet/QYGxuWFEflpORBVt

Can you help me to fix this problem and to save updated data to database?

Thanks in advance for any help.

答案1

得分: 0

通过查看您在MudBlazor Playground中添加的代码,我意识到您试图将MudSelect的值绑定到您的本地int t = 0的razor变量。

所以我在您的ProcessAreaInstructionResponse中添加了一个字段,用于存储MudSelect的值。

public class ProcessAreaInstructionResponse
{
    public Int64 Id { get; set; }
    public string Name { get; set; }
    public int BindedValue { get; set; } = 1; // 新字段
}

并且现在它可以正常工作 如何在同一页面上使用Blazor添加多个下拉列表?

英文:

By looking for the codes you add in the MudBlazor Playground, I realized that you try to bind the value of the MudSelect to your local int t = 0 razor variable.

So I added a field in your ProcessAreaInstructionResponse, to store the value of the MudSelect.

    public class ProcessAreaInstructionResponse
    {
        public Int64 Id { get; set; }
        public string Name { get; set; }
        public int BindedValue { get; set; } = 1; // new field
    }

/////

&lt;MudSelect T=&quot;int&quot; Label=@item.Name id=&quot;@item.Id&quot; @bind-Value=&quot;item.BindedValue&quot;&gt;
   @foreach (var instruction in _instructions)
   {
      &lt;MudSelectItem T=&quot;int&quot; Value=&quot;@instruction.Id&quot;&gt;@instruction.Description&lt;/MudSelectItem&gt;
   }
&lt;/MudSelect&gt;

And now it is working 如何在同一页面上使用Blazor添加多个下拉列表?

答案2

得分: -3

在Blazor中,你可以使用组件轻松在同一页上添加多个下拉列表。Blazor中的组件是可重用的UI元素,可以封装自己的逻辑和展示。以下是如何在同一页上添加多个下拉列表的方法:

创建下拉列表组件:
为你的下拉列表创建一个新的组件。让我们称它为Dropdown.razor:

<select @bind="SelectedValue">
    @foreach (var item in Items)
    {
        <option value="@item">@item</option>
    }
</select>

@code {
    [Parameter]
    public List<string> Items { get; set; }

    [Parameter]
    public string SelectedValue { get; set; }
}

在你的页面中使用下拉列表组件:
在你的Blazor页面(例如Index.razor),你可以使用Dropdown组件:

@page "/"

<h3>下拉列表</h3>

<Dropdown Items="@Items1" SelectedValue="@SelectedValue1" />
<Dropdown Items="@Items2" SelectedValue="@SelectedValue2" />

@code {
    private List<string> Items1 = new List<string> { "选项1", "选项2", "选项3" };
    private string SelectedValue1 { get; set; }

    private List<string> Items2 = new List<string> { "选择A", "选择B", "选择C" };
    private string SelectedValue2 { get; set; }
}

在这个例子中,你为要显示的每个下拉列表创建了一个Dropdown组件的新实例。你将项目列表和选定的值传递给组件的每个实例。

每个下拉列表都独立维护自己的状态。当用户在一个下拉列表中选择一个选项时,只有该下拉列表的选定值会被更新。

这种方法允许你将下拉逻辑和展示封装在一个可重用的组件中,使你的代码更有组织性和可维护性。

英文:

In Blazor, you can easily add multiple dropdown lists on the same page using components. Components in Blazor are reusable UI elements that can encapsulate their own logic and presentation. Here's how you can add multiple dropdown lists on the same page:

Create a Dropdown Component:
Create a new component for your dropdown list. Let's call it Dropdown.razor:

&lt;select @bind=&quot;SelectedValue&quot;&gt;
    @foreach (var item in Items)
    {
        &lt;option value=&quot;@item&quot;&gt;@item&lt;/option&gt;
    }
&lt;/select&gt;

@code {
    [Parameter]
    public List&lt;string&gt; Items { get; set; }

    [Parameter]
    public string SelectedValue { get; set; }
}

Use the Dropdown Component in Your Page:
In your Blazor page (Index.razor for example), you can use the Dropdown component:

@page &quot;/&quot;

&lt;h3&gt;Dropdown Lists&lt;/h3&gt;

&lt;Dropdown Items=&quot;@Items1&quot; SelectedValue=&quot;@SelectedValue1&quot; /&gt;
&lt;Dropdown Items=&quot;@Items2&quot; SelectedValue=&quot;@SelectedValue2&quot; /&gt;

@code {
    private List&lt;string&gt; Items1 = new List&lt;string&gt; { &quot;Option 1&quot;, &quot;Option 2&quot;, &quot;Option 3&quot; };
    private string SelectedValue1 { get; set; }

    private List&lt;string&gt; Items2 = new List&lt;string&gt; { &quot;Choice A&quot;, &quot;Choice B&quot;, &quot;Choice C&quot; };
    private string SelectedValue2 { get; set; }
}

In this example, you create a new instance of the Dropdown component for each dropdown list you want to display. You pass the list of items and the selected value to each instance of the component.

Each dropdown list maintains its own state independently. When the user selects an option in one dropdown, only that dropdown's selected value will be updated.

This approach allows you to encapsulate the dropdown logic and presentation in a reusable component, making your code more organized and maintainable.

huangapple
  • 本文由 发表于 2023年8月10日 22:24:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定