locked
On Post not called on return page() RRS feed

  • Question

  • 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>

    Tuesday, February 19, 2019 7:22 PM

Answers

  • User475983607 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>

    Populate the Tests collection in the Post handler.  The easiest thing to do is add the following code to the Post Handler.

    TestManagement testsypc = new TestManagement();
    Tests = testsypc.getTests();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 19, 2019 8:33 PM