I do have a Blazor App with a Product Page. There are some products in the database that I do want to display when the Products tab is selected. However the products are not displayed.
However on LoadProducts i could not get anything from res.Value, there is no such thing. How to get the list on LoadProducts method?
C#
private async Task LoadProducts()
{
try
{
var response = await ProductController.GetAllProducts();
var res = response.Result;
if (response.Value != null)
{
ProductsList = response.Value;
}
else
{
throw new Exception("Error: Unable to retrieve products.");
}
}
catch (Exception ex)
{
throw new Exception($"Error loading products: {ex.Message}");
}
}

Vishal JoshiRank #377 · 6.7K reputationPosted Jun 9, 2024, 2:25 AM EDT
Hello
Try to use ToList(). check out the below code sample
Thanks
Rajendra TaradaleRank #1,289 · 1.3K reputationPosted Jun 10, 2024, 1:09 AM EDT
Also, check if this variable assignment is happening from any other places(for example default/ new initialization) - ProductsList
Rositsa RusevaRank #5,080 · 312 reputationPosted Jun 16, 2024, 6:44 AM EDT
Maybe there should be another solution
Jaimin ShethiyaRank #177 · 13.7K reputationPosted Jun 9, 2024, 2:38 AM EDT
Hello,
Your function haven't return any list so you need to return that list.
Try below code
private async Task
> LoadProducts()
{
var productsList = new List();
try
{
var response = await ProductController.GetAllProducts();
var res = response.Result;
if (response.Value != null)
{
productsList = response.Value.ToList();
}
else
{
throw new Exception("Error: Unable to retrieve products.");
}
return productsList;
}
catch (Exception ex)
{
throw new Exception($"Error loading products: {ex.Message}");
}
}
Thanks