gpt4 book ai didi

javascript - Blazor 中的级联下拉菜单

转载 作者:行者123 更新时间:2023-11-30 23:56:57 24 4
gpt4 key购买 nike

我正在使用服务器端 Blazor - 我在国家/地区表中有国家/地区列表,其中包含两列 - CountryCode 和 CountryName。如何使用 InputSelect 显示数据以选择国家/地区名称并填充国家/地区代码这是我的 Razor 页面:

<EditForm Model="@DisplayCountry" OnValidSubmit="@InsertCountry">
<DataAnnotationsValidator />
<ValidationSummary />

<div class="col-12 row">
<label class="col-2 font-weight-bold">CountryCode:</label>
<InputSelect id="CountryCode" @bind-Value="DisplayCountry.CountryCode" />
&nbsp;<ValidationMessage For="@(() => DisplayCountry.CountryCode)" />
</div>

<div class="col-12 row">
<label class="col-2 font-weight-bold">CountryName:</label>
<InputText id="CountryName" @bind-Value="DisplayCountry.CountryName" placeholder="CountryName" />
&nbsp;<ValidationMessage For="@(() => DisplayCountry.CountryName)" />
</div>


<br />
<div class="col-12 row">
<span class="col-2"></span>
<input type="submit" class="form-control col-1 btn btn-primary" value="Save" />
</div>

最佳答案

这是一个简单的演示,如下所示:

型号:

namespace BlazorApp1.Models
{
public class Country
{
public string CountryCode { get; set; }
public string CountryName { get; set; }
}
}

Razor :

@page "/counter"
@using BlazorApp1.Models
@using BlazorApp1.Data
@inject CountryService countryService

<EditForm Model="@DisplayCountry">
<DataAnnotationsValidator />
<ValidationSummary />

<div class="col-12 row">
<label class="col-2 font-weight-bold">CountryCode:</label>
<InputSelect @bind-Value="@coutryName" class="form-control">
@foreach (var cnt in DisplayCountry)
{
<option value="@cnt.CountryName">@cnt.CountryCode</option>
}

</InputSelect>
&nbsp;<ValidationMessage For="@(() => DisplayCountry[0].CountryCode)" />
</div>

<div class="col-12 row">
<label class="col-2 font-weight-bold">CountryName:</label>
<InputText id="CountryName" @bind-Value="@coutryName" placeholder="CountryName" class="form-control"/>
&nbsp;<ValidationMessage For="@(() => DisplayCountry[0].CountryName)" />
</div>


<br />
<div class="col-12 row">
<span class="col-2"></span>
<input type="submit" class="form-control col-1 btn btn-primary" value="Save" />
</div>
</EditForm>
@code
{
string coutryName;
List<Country> DisplayCountry = new List<Country>();
protected override void OnInitialized()
{
DisplayCountry = countryService.GetCountry();
}
}

服务:

namespace BlazorApp1.Data
{
public class CountryService
{
public List<Country> GetCountry()
{
//for easy testing,I just hard-coded assignment
//you could get the data from database like
//_context.Country.ToList()
var data = new List<Country>()
{
new Country() { CountryCode="1011", CountryName="USA"},
new Country() { CountryCode="1021", CountryName="Africa"},
new Country() { CountryCode="1031", CountryName="China"},
new Country() { CountryCode="1041", CountryName="UK"},

};
return data;
}
}
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
//register the service
services.AddSingleton<CountryService>();
}

结果: enter image description here

如果您不想从服务获取数据,您可以播种类似 here 的数据.

关于javascript - Blazor 中的级联下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60981805/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com