英文:
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 = "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>
}
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; // 新字段
}
并且现在它可以正常工作
英文:
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
}
/////
<MudSelect T="int" Label=@item.Name id="@item.Id" @bind-Value="item.BindedValue">
@foreach (var instruction in _instructions)
{
<MudSelectItem T="int" Value="@instruction.Id">@instruction.Description</MudSelectItem>
}
</MudSelect>
And now it is working
答案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:
<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; }
}
Use the Dropdown Component in Your Page:
In your Blazor page (Index.razor for example), you can use the Dropdown component:
@page "/"
<h3>Dropdown Lists</h3>
<Dropdown Items="@Items1" SelectedValue="@SelectedValue1" />
<Dropdown Items="@Items2" SelectedValue="@SelectedValue2" />
@code {
private List<string> Items1 = new List<string> { "Option 1", "Option 2", "Option 3" };
private string SelectedValue1 { get; set; }
private List<string> Items2 = new List<string> { "Choice A", "Choice B", "Choice C" };
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论