I'm trying to load the below table, however, the drop-down menu for the school is not loading. It looks to me like it's null. And I figured that the InputSelect doesn't work with an INT ID yet in Blazor.
I filled the below for and the country code was populated from a country table as a lookup based on the country chosen from the InputSelect column, "Country Name", circled in blue. When I select the country, I expect it to load the correspionding countrycode but not the case.
Razor Page
@using ITSM.Data
@using ITSM.Services
@inject ISchoolService service
@inject IJSRuntime jsRuntime
<div class="modal" tabindex="-1" role="dialog" id="schoolmodal">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">School Detail</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@if (CountryList is null)
{
<p><em>Loading...</em></p>
}
else
{
<h4>Schools</h4>
<EditForm Model="@SchoolObject" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="Location" CountryCode">Country Code:</label>
<InputSelect @bind-Value="@SchoolObject.CountryCode" class="form-control">
<option value="0">Select</option>
@foreach (var item in CountryList)
{
<option value="@item.CountryCode">@item.CountryName</option>
}
</InputSelect>
<ValidationMessage For="@(() => @CountryObject.CountryCode)" />
}
</div>
<br />
<div class="form-group">
<label class="col-2 font-weight-bold">Country Code:</label>
<InputText id="CountryCode" @bind-Value="@SchoolObject.CountryCode" class="form-control" placeholder="CountryCode" />
<ValidationMessage For="@(() => SchoolObject.CountryCode)" />
</div>
Tables Structure
CREATE TABLE [dbo].[Country](
[CountryID] [int] IDENTITY(1,1) NOT NULL,
[CountryCode] [char](3) NOT NULL,
[CountryName] [varchar](255) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[School](
[SchoolID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NOT NULL,
[Location] [nvarchar](50) NOT NULL,
[Address] [nvarchar](50) NULL,
[PostCode] [nvarchar](10) NULL,
[CountryCode] [char](3) NOT NULL,
[SchoolAdminPersonID] [int] NOT NULL,
CONSTRAINT [PK_School] PRIMARY KEY CLUSTERED
(
[SchoolID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
<button type="submit" class="btn btn-primary">Submit</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
</EditForm>
}
</div>
</div>
</div>
</div>
@code {
private List<CountryModel> CountryList;
//private List<SchoolModel> SchoolList;
[Parameter]
public SchoolModel CountryObject { get; set; }
[Parameter]
public SchoolModel SchoolObject { get; set; }
protected string schoold = string.Empty;
[Parameter]
public Action DataChanged { get; set; }
private async Task Closeschoolmodal()
{
await jsRuntime.InvokeAsync<object>("CloseModal", "schoolmodal");
}
private async void HandleValidSubmit()
{
if (SchoolObject.SchoolID == 0)
{
await service.Add(SchoolObject);
}
else
{
await service.Update(SchoolObject);
}
await Closeschoolmodal();
DataChanged?.Invoke();
}
}
SchoolList
@page "/SchoolList"
@using ITSM.Shared
@using ITSM
@using ITSM.Data
@using ITSM.Services
@inject ISchoolService service
@inject IJSRuntime jsRuntime
<h1>School</h1>
<p>Countries List.</p>
@if (SchoolLists == null)
{
<p><em>Loading...</em></p>
}
else
{
<br>
<div>
<input type="button" data-toggle="modal" data-target="#schoolmodal" class="btn btn-primary" value="Add School" @onclick="(() => InitializeTaskObject())" />
</div>
<br/>
<table class="table">
<thead>
<tr>
<th>SchoolID</th>
<th>Name</th>
<th>Location</th>
<th>Address</th>
<th>PostCode</th>
<th>CountryCode</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach (var SchoolItem in SchoolLists)
{
<tr>
<td>@SchoolItem.SchoolID</td>
<td>@SchoolItem.Name</td>
<td>@SchoolItem.Location</td>
<td>@SchoolItem.Address</td>
<td>@SchoolItem.PostCode</td>
<td>@SchoolItem.CountryCode</td>
<td><input type="button" class="btn btn-primary" @onclick="(() => PrepareForEdit(SchoolItem))" data-toggle="modal" data-target="#schoolmodal" value="Edit"></td>
<td><input type="button" class="btn btn-danger" @onclick="(() => PrepareForDelete(SchoolItem))" data-toggle="modal" data-target="#confirmDeleteModal" value="Delete" /></td>
</tr>
}
</tbody>
</table>
}
<ConfirmDialog OnClick="@Delete" />
<SchoolDetail SchoolObject=SchoolObject DataChanged="@DataChanged"></SchoolDetail>
@code {
List<SchoolModel> SchoolLists;
SchoolModel SchoolObject = new SchoolModel();
private void PrepareForEdit(SchoolModel School)
{
SchoolObject = School;
}
private void PrepareForDelete(SchoolModel School)
{
SchoolObject = School;
}
private async void Delete()
{
var School = await service.Delete(SchoolObject.SchoolID);
await jsRuntime.InvokeAsync<object>("Closemodal", "confirmDeletemodal");
SchoolLists = await service.Get();
SchoolObject = new SchoolModel();
StateHasChanged();
}
protected override async Task OnInitializedAsync()
{
SchoolLists = await service.Get();
}
private void InitializeTaskObject()
{
SchoolObject = new SchoolModel();
}
private async void DataChanged()
{
SchoolLists = await service.Get();
StateHasChanged();
}
}
[![Snapshot of the dropdown menu][1]][1]
I see the problem yet. In your SchoolDetail your CountryList is null. You don't assign the CountryList by parameter or somewhere else in SchoolDetail from parent object. You must load/create one in SchoolDetail or in inject from SchoolList by parameter a loaded/created list. In JavaScript are (sometimes) created variables in subcode visible. In Blazor/C# you must transport it from one to another.
Here a Example. Change in SchoolDetail
//private List<SchoolModel> SchoolList;
[Parameter] public List<CountryModel> CountryList { get; set; } = null;
And change in SchoolList.
<SchoolDetail SchoolObject="@SchoolObject" CountryList="@YourCountryList" DataChanged="@DataChanged"></SchoolDetail>
I don't see your CountryList there. You must Load or Create one there.
I recommend to write the objects this way, then you will find them later better in the code with one look.
SchoolObject="@SchoolObject"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.