为什么我不能导入在Cities.razor中定义的城市值?


@page "/city"
@page "/city/{CityId}"
@inject ICityService CityService
@if (CityId == null)
{
<PageTitle>Add a new City</PageTitle>
<h3>Add a new City</h3>
}
else
{
<Pagetitle>Edit @city.CityName</Pagetitle>
<h3>Edit @city.CityName</h3>
}
@code
{
[Parameter]
public int? CityId { get; set; }
City city = new City { Country = new Country() };
}
@page "/cities"
@inject ICityService CityService
@inject NavigationManager NavigationManager
<PageTitle>Citie overview</PageTitle>
<h3>City Overview</h3>
<table class="table">
<thead>
<tr>
<th> City Id</th>
<th> City Name</th>
<th> City Abbrevation</th>
<th> Country</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var city in CityService.Cities)
{
<tr>
<td>@city.CityId</td>
<td>@city.CityName</td>
<td>@city.CityAbbrevation</td>
<td>@city.Country.CountryName</td>
<td>
<button class="btn btn-primary" @onclick="(() => ShowCity(city.CityId))"><i class="oi oi-pencil"></i></button>
</td>
</tr>
}
</tbody>
</table>
<button class="btn btn-primary" @onclick="AddNewCity">Create New City</button>
@code {
protected override async Task OnInitializedAsync()
{
await CityService.GetCities();
}
void ShowCity(int CityId)
{
NavigationManager.NavigateTo($"city/{CityId}");
}
void AddNewCity()
{
NavigationManager.NavigateTo("/city");
}
}

在第一页中,我试图使用与第二页相同的变量。

<td>@city.CityId</td>
<td>@city.CityName</td>
<td>@city.CityAbbrevation</td>
<td>@city.Country.CountryName</td>

但是我不能将变量导入到下面的页面,我不确定我做错了什么。

@page "/city"
@page "/city/{CityId}"
@inject ICityService CityService
@if (CityId == null)
{
<PageTitle>Add a new City</PageTitle>
<h3>Add a new City</h3>
}
else
{
<Pagetitle>Edit @city.CityName</Pagetitle>
<h3>Edit @city.CityName</h3>
}
@code
{
[Parameter]
public int? CityId { get; set; }
City city = new City { Country = new Country() };
}

我正在跟随Patrick god在youtube上的教程:https://www.youtube.com/watch?v=K_P-qJj_8Bg&t=4211s&ab_channel=PatrickGod (min 40-44)。

我已经生成了一个模型、控制器和服务。API工作,但一旦我尝试创建和编辑页面它不工作。

如果需要更多的信息,请告诉我。

您需要在第二页上调用CityService,因为您只接收到CityId,而不是所有相关的城市数据,如下所示:

protected override async Task OnInitializedAsync()
{
city = await CityService.GetCity(CityId);
}

相关内容

  • 没有找到相关文章

最新更新