User1352447851 posted
I am trying to get a List from another class using onGet() and use it to populate a dropdown. The code below works at first, however if the Model is invalid elsewhere and I return the page, I get an error saying that Tests is null. Ie:
if (!ModelState.IsValid)
{
return Page(); //Returns the page but Tests becomes null.
}
If I try using binding I get a blank list if the page is returned.
How to I make the list persistent through Page() calls or retrieved every time? I didn't want to use a database as the list can change.
In my Page Model:
public List<Tests> Tests {
get; set; }
public void OnGet()
{
TestManagement testsypc =
new TestManagement();
Tests = testsypc.getTests();
}
CSHTML:
<label asp-for="Cats.Breed"
class="control-label">Cat Breed</label>
<select
asp-for="Cats.Breed" class="form-control"
>
@foreach (var item in Model.Tests)
{
<option value=@item.DisplayName>@Html.DisplayFor(modelItem => item.DisplayName)</option>
}
</select>