根据选择列表的值更改文本框的值



我已经搜索了2个多小时的方法,但仍然没有找到解决方案:(。我使用的是Razor组件,我想显示注册Student的详细信息。因此,如果选择了Month,则应该显示一个带有Semester的文本框。例如,如果从列表中选择了月份1(1月(,学期文本框应显示1(学期编号(。

学生模型:

public partial class StudentDetails
{
public int StudentNumber{ get; set; }
public int StudentAge{ get; set; }
public int Month{ get; set; }
public int Semester{ get; set; }
public string FName { get; set; }
}

注册模式:

public partial class Reg
{
public int Month{ get; set; }
public int Semester{get; set;  }
}

选择框:

<MudSelect  @bind-Value="student.Month" For="@(()=>Student.Month)" T="int"  Label="Student Joining Month" AnchorOrigin="Origin.BottomCenter" Required="true">
@foreach (var item in num)
{
<MudSelectItem T="int" Value="@item.Month">@item.Month</MudSelectItem>
}  
</MudSelect>

这就是您想要做的吗?

<MudSelect Value="@student.Month" ValueChanged="@OnMonthChanged" T="int" Label="Month" AnchorOrigin="Origin.BottomCenter" Required="true">
@foreach (var registration in registrations)
{
<MudSelectItem T="int" Value="@registration.Month">@registration.Month</MudSelectItem>
}  
</MudSelect>
@* should be readonly *@
<MudTextField @bind-Value="@student.Semester" Label="Semester" ReadOnly="true" Variant="Variant.Text" />

@code {
private List<Reg> registrations;
private StudentDetails student = new();
private void OnMonthChanged(int selectedMonth)
{
student.Month = selectedMonth;

// get the registration based on the selected month
var registration = registrations.Single(r => r.Month == selectedMonth);

// set the semester of student details
student.Semester = registration.Semester;
}
}

相关内容

  • 没有找到相关文章