英文:
Why am I not able to import the city values which are defined in 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");
}
}
In the first page, I am trying to use the same variables as in the second page.
<td>@city.CityId</td>
<td>@city.CityName</td>
<td>@city.CityAbbrevation</td>
<td>@city.Country.CountryName</td>
But I am not able to import the variables to the below page, and I am not sure what I am doing wrong.
@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() };
}
I am following a tutorial on YouTube by Patrick God: https://www.youtube.com/watch?v=K_P-qJj_8Bg&t=4211s&ab_channel=PatrickGod (min 40-44).
I've generated a model, controllers, and services. The API works, but once I try to make a create and edit page, it doesn't work.
Please let me know if more information is required.
<details>
<summary>英文:</summary>
@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");
}
}
In the first page I am trying to use the same variables as in second page.
<td>@city.CityId</td>
<td>@city.CityName</td>
<td>@city.CityAbbrevation</td>
<td>@city.Country.CountryName</td>
But I am not able to import the variables to the below page and I am not sure what I am doing wrong.
@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() };
}
I am following a tutorial on youtube by Patrick god: <https://www.youtube.com/watch?v=K_P-qJj_8Bg&t=4211s&ab_channel=PatrickGod> (min 40-44).
I've generated a model, controllers and services. The API works, but once I try to make an create and edit page it doesn't work.
Please let me know if more information is required.
</details>
# 答案1
**得分**: 1
你需要在第二页调用 CityService,因为你只接收到了 CityId,而没有获取到所有相关的城市数据,就像这样:
```csharp
protected override async Task OnInitializedAsync()
{
city = await CityService.GetCity(CityId);
}
英文:
You need to call the CityService on the second page because you are only receiving the CityId and not all the related city data, like this:
protected override async Task OnInitializedAsync()
{
city = await CityService.GetCity(CityId);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论