【问题标题】:Cascading Dropdown in BlazorBlazor 中的级联下拉菜单
【发布时间】:2020-04-01 23:18:11
【问题描述】:

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

<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>

【问题讨论】:

  • 您是否尝试过在控制器中使用 ViewBag 和 ViewData?
  • 不清楚您需要帮助解决问题的哪一部分 - 您是否设法从数据库中获取列表?如果是这样,请取消标记 SQL Server。您是否尝试将数据传递给客户端?您是否尝试在客户端上显示数据?您是否正在尝试编写一些客户端代码?请澄清,以便有人可以帮助您。
  • 我正在使用服务器端 Blazor - 我在国家/地区表中有我的国家/地区列表,其中包含两列 - CountryCode 和 CountryName。如何使用 InputSelect 显示数据以选择国家名称并填充国家代码?
  • 请直接在问题中添加任何说明。并删除 SQL Server 标记,因为它不是 SQL Server 问题。

标签: javascript c# asp.net-core blazor-server-side


【解决方案1】:

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

型号:

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

剃须刀:

@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>();
}

结果:

如果你不想从服务中获取数据,你可以像here这样播种数据。

【讨论】:

  • 我实现了这个,但我收到以下错误消息:严重性代码描述项目文件行抑制状态错误 CS0029 无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”。 Collections.Generic.List' 和:严重性代码描述项目文件行抑制状态错误 CS0117“国家”不包含“国家名称”DataAccessLibrary C:\Projects\ITSMCourses\DataAccessLibrary\CountryData.cs 的定义18 活跃。
  • 你确定你的命名空间是正确的吗?没有必要像我一样改变你的命名空间。
  • 另外,你好像有两个项目?你能分享更详细的代码吗?
  • 当数据从下拉菜单硬编码时,我能够填充数据,但不能从数据库表中填充。
  • 感谢 Rena,现在可以使用了。事实证明,@foreach 变量是从我这边错误地应用的。
猜你喜欢
  • 1970-01-01
  • 2011-03-03
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
相关资源
最近更新 更多