My RandomValue class and WeatherForecast class are causing me some trouble. The WeatherForecast class is functioning properly, populating the table with data as expected. However, the RandomValues class/interface appears to be returning a list of objects without any properties. This results in a table with the correct number of rows but no corresponding properties. I am in need of assistance to resolve this issue.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace WebApplication14.Models
{
public class RandomValue
{
public RandomValue RandomVal(JObject obj)
{
RandomValue val = new RandomValue();
val.userId = Int32.Parse(obj.GetValue("userId").ToString());
val.id = Int32.Parse(obj.GetValue("id").ToString());
val.title = obj.GetValue("title").ToString();
val.completed = obj.GetValue("completed").ToString();
return val;
}
Int32 userId { get; set; }
Int32 id { get; set; }
String title { get; set; }
String completed { get; set; }
}
}
using System;
namespace WebApplication14
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
}
Here are their respective controllers:
public class RandomValuesController : ControllerBase
{
public RandomValuesController(ILogger<RandomValuesController> logger)
{
Logger = logger;
}
public ILogger<RandomValuesController> Logger { get; }
[HttpGet]
public IEnumerable<RandomValue> Get()
{
using var httpClient = new WebClient();
Int32 NumberRows = new Random().Next(1, 10);
List<RandomValue> Values = new List<RandomValue>();
for (Int32 row = 0; row < NumberRows; row++)
{
Int32 randomRow = new Random().Next(1, 200);
JObject randomJSON = JObject.Parse(httpClient.DownloadString("https://jsonplaceholder.typicode.com/todos/" + randomRow));
RandomValue value = new RandomValue().RandomVal(randomJSON);
Values.Add(value);
}
RandomValue[] theValues = Values.ToArray();
return theValues;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace WebApplication14.Controllers
{
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
var example = Enumerable.Range(1, 5).Select(
index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
return example;
}
}
}
Both of these controllers return an array of the specified type, and the models and controllers seem to function correctly.
Now let's take a look at my TypeScript file:
import { Component, Inject } from '@angular/core';
import { HttpClient } from '@common/http';
@Component({
selector: 'app-fetch-data',
templateUrl: './fetch-data.component.html'
})
export class FetchDataComponent {
public forecasts: WeatherForecast[];
public randomvalues: RandomValue[];
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<WeatherForecast[]>(baseUrl + 'weatherforecast')
.subscribe(result => { this.forecasts = result; }, error => console.error(error));
http.get<RandomValue[]>(baseUrl + 'randomvalues')
.subscribe(result => { this.randomvalues = result; }, error => console.error(error));
}
}
interface WeatherForecast {
date: string;
temperatureC: number;
temperatureF: number;
summary: string;
}
interface RandomValue {
userId: number;
id: number;
title: string;
completed: string;
}
And here is my HTML file:
<h1 id="tableLabel">Weather Forecast</h1>
<p>This component showcases fetching data from the server.</p>
<p *ngIf="!forecasts"><em>Loading...</em></p>
<table class='table table-striped' aria-labelledby="tableLabe" *ngIf="forecasts">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let forecast of forecasts">
<td>{{ forecast.date }}</td>
<td>{{ forecast.temperatureC }}</td>
<td>{{ forecast.temperatureF }}</td>
<td>{{ forecast.summary }}</td>
</tr>
</tbody>
</table>
<table class='table table-striped' aria-labelledby="tableLabel" *ngIf="randomvalues">
<thead>
<tr>
<th>UserId</th>
<th>Id</th>
<th>Title</th>
<th>Completed</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let value of randomvalues">
<td>{{ value.userId }}</td>
<td>{{ value.id }}</td>
<td>{{ value.title }}</td>
<td>{{ value.completed }}</td>
</tr>
</tbody>
</table>
The WeatherForecasts consist of Objects with the correct properties, while the RandomValues do not contain any properties within the Objects.