I get this error: Could not find 'ShowToastr' ('ShowToastr' was undefined). I am creating a Blazor app with .NET6. This is the page:
C#
@page "/hotel/rooms"
@inject ILocalStorageService localStorage
@inject IJSRuntime jsRuntime
@inject IHotelRoomService hotelRoomService
HotelRooms
@code {
private HomeViewModel HomeModel { get; set; } = new HomeViewModel();
public IEnumerable Rooms { get; set; } = new List();
protected override async Task OnInitializedAsync()
{
try
{
if (await localStorage.GetItemAsync(SD.Local_InitialBooking) != null)
{
HomeModel = await localStorage.GetItemAsync(SD.Local_InitialBooking);
}
else
{
HomeModel.NumberOfNights = 1;
}
await LoadRooms();
}
catch (Exception ex)
{
await jsRuntime.ToastrError(ex.Message);
}
}common.js:
C#
window.ShowToastr = (type, message) => {
if (type === "success") {
toastr.success(message, "Operation Successful")
}
if (type === "error") {
toastr.error(message, "Operation Failed")
}
}I also have two references on ToastrError:
C#
public static class IJSRuntimeExtension
{
public static async ValueTask ToastrSuccess(this IJSRuntime JSRuntime, string message)
{
await JSRuntime.InvokeVoidAsync("ShowToastr", "success", message);
}
public static async ValueTask ToastrError(this IJSRuntime JSRuntime, string message)
{
await JSRuntime.InvokeVoidAsync("ShowToastr", "error", message);
}
}
Rositsa RusevaRank #5,080 · 312 reputationPosted Sep 14, 2022, 12:52 PM EDT
While debugging HotelRooms.razor the whole try block passes, the LoadRooms(the method is entered) and after that the debugger enters the catch block where I get this error.
This is the index.html file: