I have created a DropDownList box component which is bound to a long list of vendors.
ContractVendorSelect.razor
<div class="k-form-field-wrap">
@if (IsLoaded)
{
<select @bind=Contract.Vendor>
<option>-- Select</option>
@foreach (var item in VendorData)
{
<option value="@item.Value">@item.Text</option>
}
</select>
}
else
{
<span>Loading vendors ... </span>
}
</div>
I have placed my C# code in a code behind file.
ContractVendorSelect.razor.cs
public partial class ContractVendorSelect : FormFieldBase
{
[Parameter]
public Contract? Contract { get; set; }
[Parameter]
public string? Id { get; set; } = null;
[Parameter]
public string? Label { get; set; } = null;
[Parameter]
public Sites? Site { get; set; } = null;
protected bool IsLoaded { get; set; } = false;
internal List<VendorSelectData>? VendorData { get; set; } = new List<StiVendorSelectData>();
private async Task SetVendorList()
{
if (Site != null)
{
var stagedData = await VendorSearch.GetVendorsAsync(this.Site.DBName);
VendorData = new List<VendorSelectData>();
foreach(Vendor vendor in stagedData)
{
VendorData.Add(new VendorSelectData(vendor.Vendor_Name, vendor.Vendor_Key, vendor));
}
IsLoaded = true;
}
}
protected async override Task OnInitializedAsync()
{
await this.SetVendorList();
StateHasChanged();
}
internal class VendorSelectData
{
public long Value { get; set; }
public String Text { get; set; }
public Vendor Vendor { get; set; }
public VendorSelectData(string text, long value, Vendor model)
{
Text = text;
Value = value;
Vendor = model;
}
}
}
The problem I have is that the component does not re-render after the IsLoaded flag has been set to true (indicating that the list has been successfully retrieved and is ready to render). I have tried implementing "StateHasChanged" in a number of places but it continues to simply display the "Loadin vendors ... " message.
Any help or a point in the right direction would be greatly appreciated.
After reviewing this I failed to add the implementation of the component in a test page ...
Test.razor
<ContractVendorSelect Id="ddlContractVendor"
Label="Vendor"
Contract="contract"
Site="site">
</ContractVendorSelect>
@code {
protected Contract contract { get; set; }
protected Sites site { get; set; }
public async Task SetContract()
{
contract = await ContractSearch.FindContract(<contractID>);
}
public async Task SetSite()
{
site = await SiteSearch.GetSiteByIDAsync(<SiteID>);
}
protected override async Task OnInitializedAsync()
{
await SetSite();
await SetContract();
}
}