为什么我无法导入在Cities.razor中定义的城市数值?

huangapple go评论59阅读模式
英文:

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(&quot;/city&quot;);
}

}


In the first page I am trying to use the same variables as in second page.

             &lt;td&gt;@city.CityId&lt;/td&gt;
            &lt;td&gt;@city.CityName&lt;/td&gt;
            &lt;td&gt;@city.CityAbbrevation&lt;/td&gt;
            &lt;td&gt;@city.Country.CountryName&lt;/td&gt;

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: &lt;https://www.youtube.com/watch?v=K_P-qJj_8Bg&amp;t=4211s&amp;ab_channel=PatrickGod&gt; (min 40-44).

I&#39;ve generated a model, controllers and services. The API works, but once I try to make an create and edit page it doesn&#39;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);
}

huangapple
  • 本文由 发表于 2023年1月9日 19:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定