Presently, I am getting the following error when trying to dynamically populate a Drop Down List using Blazor.
I believe that I have created the TeamModel correctly and have referenced it the page where the code is to be populated.
Also, there are no errors on the pages, and the .Razor page can see that the TeamModel model exists.
Where am I going wrong?
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Cannot provide a value for property
'TeamModel' on type 'WasmInterface.Pages.Home'. There is no registered service of type
'WasmInterface.Models.TeamModel'. System.InvalidOperationException: Cannot provide a value for property 'TeamModel' on type 'WasmInterface.Pages.Home'. There is no registered service of type 'WasmInterface.Models.TeamModel'.'
This is the code for the control in the page
@page "/"
@* //@using WasmInterface.Services *@
@using WasmInterface.Models
@inject NavigationManager Navigation
@inject TeamModel TeamModel
<EditForm >
<div class="form-group col-md-4" >
<label class="col-form-label" for="Team">Team : @TeamModel.ID</label>
<option value="0" selected>Please Select Team</option>
@foreach (var item in teams)
{
<option value="@item.ID">@item.Name</option>
}
</div>
</EditForm>
This is the code in the .cs file for the page
#region Uses
using Microsoft.AspNetCore.Components;
using WasmInterface.Models;
#endregion
namespace WasmInterface.Pages;
/// <inheritdoc />
/// <summary>
/// This is the code file that is 'behind' the Home.razor page
/// </summary>
public sealed partial class Home
{
[Inject]
private IConfiguration? Config { get; init; }
private string PageTitle => Config?[ "Home:PageTitle" ] ?? string.Empty;
#pragma warning disable IDE0055
/// The code that's violating the rule is on this line.
#pragma warning restore IDE0055
List<TeamModel> teams = new List<TeamModel>(){
new TeamModel() { ID = 1, Name = "Team"},
new TeamModel() { ID = 2, Name = "Team2"},
new TeamModel() { ID = 3, Name = "Team3"}
};
}
And this is the code for the Model:
/// <summary>
/// Gets Team Model
/// </summary>
public class TeamModel
{
///// <summary>
///// Gets ID
///// </summary>
public int ID { get; set; }
///// <summary>
///// Gets Name
///// </summary>
public string? Name { get; set; }
}