如果在Blazor中更改了另一个选择框,则如何重置选择框的选择(带到Adefault值)?
我的大火页面上有两个选择的盒子。我在SELECT BOX-1上选择部门和Select Box-2上的部门相关机器组(Select Box-2列表将加载ACC。在Select-Box-1上进行选择)。通常,它正在工作。但是我有以下问题: 如果我在SB-1中选择部门(MFT),然后选择SB-2中的机器组3。 将SB-1的选择更改为另一个部门: SB-2中列出了属于新部门的正确列表,但我直接看到了3。 SB-2的NWE选择列表。通常,我希望SB-2应该重置为默认值( - 选择机器组 - )我该怎么做?换句话说:如何将具有代码的选择框设置为默认或预定义的选择?
@page "/connect"
@using System.IO
<select class="Dep" @onchange="func_dep">
<option value="">-- Select Department --</option>
@foreach (var dept in templates_dep)
{
<option value=@dept>@dept</option>
}
</select>
<select class="MG" @onchange="func_MG">
<option value="">-- Select Machine Group --</option>
@foreach (var mgt in templates_MG)
{
<option value=@mgt>@mgt</option>
}
</select>
@code{
List<string> templates_dep = new List<string>() { "",""};
protected override async Task OnInitializedAsync()
{
templates_dep.Clear();
read_dep();
}
public void read_dep()
{
var dep_file = File.ReadAllLines("files\\mae\\dep.csv");
foreach (var s in dep_file)
templates_dep.Add(s);
}
}
@functions {
string selectedString_dep{get; set; }
List<string> templates_MG = new List<string>() { "", "", "", "", "" };
string selectedString_MG {get; set; }
async void func_dep(ChangeEventArgs e)
{
templates_MG.Clear();
var path_mg ="files\\mae\\"+selectedString_dep+"_MG.csv";
var logFile = File.ReadAllLines(path_mg);
foreach (var s in logFile) templates_MG.Add(s);
}
}
I have two select boxes in my Blazor page. I select the department on Select Box-1 and the department related Machine Group on Select Box-2 (List of Select Box-2 will be loaded acc. to selection on Select-Box-1). In general it is working. But I have following problem:
If I select Department (MFT) in SB-1 and select the 3. selection of Machine Group in SB-2 and then
change the selection in SB-1 to another department:
The correct list belonging to the new department is listed in SB-2, but I see directly the 3. selection
of the SB-2's nwe selection list. Normaly I would expect that SB-2 should be reset to a default value (-- Select Machine Group--) How can I do that? In other words: How can I set the selection of a selection box with code to a default or predefined selection?
@page "/connect"
@using System.IO
<select class="Dep" @onchange="func_dep">
<option value="">-- Select Department --</option>
@foreach (var dept in templates_dep)
{
<option value=@dept>@dept</option>
}
</select>
<select class="MG" @onchange="func_MG">
<option value="">-- Select Machine Group --</option>
@foreach (var mgt in templates_MG)
{
<option value=@mgt>@mgt</option>
}
</select>
@code{
List<string> templates_dep = new List<string>() { "",""};
protected override async Task OnInitializedAsync()
{
templates_dep.Clear();
read_dep();
}
public void read_dep()
{
var dep_file = File.ReadAllLines("files\\mae\\dep.csv");
foreach (var s in dep_file)
templates_dep.Add(s);
}
}
@functions {
string selectedString_dep{get; set; }
List<string> templates_MG = new List<string>() { "", "", "", "", "" };
string selectedString_MG {get; set; }
async void func_dep(ChangeEventArgs e)
{
templates_MG.Clear();
var path_mg ="files\\mae\\"+selectedString_dep+"_MG.csv";
var logFile = File.ReadAllLines(path_mg);
foreach (var s in logFile) templates_MG.Add(s);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过两种方式绑定和C#属性设置器进行操作:
You can do it with two way binding and C# property setters :
这是我组合的“级联选择”的演示系统,以回答另一个网站上的类似问题。
演示页面:
及其使用的数据集。
Here's a demo system for a "Cascading Select" that I put together to answer a similar question on another site.
The demo page:
And the data set it uses.
我发现最容易使用ijsruntime并调用纯JavaScript函数将其重置。
例如:
这是JavaScript函数:
这是从剃须刀页面调用JavaScript函数的方式:
I found it easiest to use IJSRuntime and call a pure javascript function to reset it.
For example:
Here is the javascript function:
Here is how the javascript function is called from the razor page: