Asp.net mvc Blazor应用程序输入选择不从数据库中提取
我试图从数据库表中提取国家列表,但没有显示结果集,表中有数据。下面是我所做的。虽然我是第一次使用InputSelect字段,所以我还没有完全理解它。 我能够通过输入文本来提取数据。所以看起来我的数据库连接字符串很好Asp.net mvc Blazor应用程序输入选择不从数据库中提取,asp.net-mvc,asp.net-core,blazor,blazor-server-side,blazor-client-side,Asp.net Mvc,Asp.net Core,Blazor,Blazor Server Side,Blazor Client Side,我试图从数据库表中提取国家列表,但没有显示结果集,表中有数据。下面是我所做的。虽然我是第一次使用InputSelect字段,所以我还没有完全理解它。 我能够通过输入文本来提取数据。所以看起来我的数据库连接字符串很好 //Razor Page @inject ICountryData _db <h1>Choose a Country</h1>
//Razor Page
@inject ICountryData _db
<h1>Choose a Country</h1>
<h4>Countries</h4>
@if (Countries is null)
{
<p><em>Loading...</em></p>
}
else
{
<div class="col-6">
<EditForm Model="@DisplayCountry">
<div class="form-group">
<label class="col-2 font-weight-bold">CountryName:</label>
<InputSelect @bind-Value="DisplayCountry.CountryName" class="form-control">
<option value="">Select</option>
@foreach (var person in Countries)
{
<option value="@DisplayCountry.CountryName"> @DisplayCountry.CountryCode </option>
}
</InputSelect>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</EditForm>
</div>
}
@code {
private List<CountryModel> Countries;
private DisplayCountryModel DisplayCountry = new DisplayCountryModel();
protected override async Task OnInitializedAsync()
{
Countries = await _db.GetCountry();
}
private async Task InsertCountry()
{
CountryModel C = new CountryModel
{
CountryCode = DisplayCountry.CountryCode,
CountryName = DisplayCountry.CountryName,
};
await _db.InsertCountry(C);
Countries.Add(C);
DisplayCountry = new DisplayCountryModel();
}
}
using DataAccessLibrary.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace DataAccessLibrary
{
public class CountryData : ICountryData
{
private readonly ISqlDataAccess _db;
public CountryData(ISqlDataAccess db)
{
_db = db;
}
public Task<List<CountryModel>> GetCountry()
{
string sql = "select * from dbo.Country";
return _db.LoadData<CountryModel, dynamic>(sql, new { });
}
public Task InsertCountry(CountryModel Country)
{
string sql = @"insert into dbo.Country (CountryCode, CountryName)
values (@CountryCode, @CountryName);";
return _db.SaveData(sql, Country);
}
}
}
//剃须刀页面
@注入ICountryData_db
选择一个国家
国家
@如果(国家/地区为空)
{
加载
}
其他的
{
国家名称:
挑选
@foreach(各国的var人员)
{
@DisplayCountry.CountryCode
}
提交
}
@代码{
私人名单国家;
私有DisplayCountryModel DisplayCountry=新的DisplayCountryModel();
受保护的重写异步任务OnInitializedAsync()
{
Countries=wait_db.GetCountry();
}
专用异步任务InsertCountry()
{
CountryModel C=新CountryModel
{
CountryCode=DisplayCountry.CountryCode,
CountryName=DisplayCountry.CountryName,
};
等待(C);
添加(C);
DisplayCountry=新的DisplayCountryModel();
}
}
使用DataAccessLibrary.Models;
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用System.Threading.Tasks;
命名空间DataAccessLibrary
{
公共类CountryData:ICountryData
{
私有只读ISqlDataAccess_db;
公共国家数据(ISqlDataAccess数据库)
{
_db=db;
}
公共任务GetCountry()
{
string sql=“select*from dbo.Country”;
返回_db.LoadData(sql,新{});
}
公共任务InsertCountry(CountryModel国家/地区)
{
字符串sql=@“插入dbo.Country(CountryCode,CountryName)
值(@CountryCode,@CountryName);“;
返回_db.SaveData(sql,国家/地区);
}
}
}
foreach变量错误。我把它改写成下面的样子,它成功了
<InputSelect @bind-Value="DisplayCountry.CountryName" class="form-control">
<option value="">Select</option>
@foreach (var item in Countries)
{
<option value="@item.CountryCode"> @item.CountryName </option>
}
</InputSelect>
挑选
@foreach(国家/地区的var项目)
{
@item.CountryName
}