ASP.NET Core Razor pages web app: in the OnPost the request has data in the Items list but in the OnGet the list is empty

What am I doing wrong?

I have not used ASP.NET Core or EF Core in a long time so I’m relatively new to it.

When I debug with breakpoints in the OnPost the Items list has data, but when the page is redirected and loads the Items is null.

I am using EF Core in-memory to store the data, it’s mostly a simple page but I can’t seem to get it to work.

Any help much appreciated.

These are my model classes:

[PrimaryKey("OrderId")]
public class Request
{
    public string? OrderId { get; set; }
    public string? SalesPerson { get; set; }
    public string? OrderConfirmedDate { get; set; }
    public string? CurrencyName { get; set; }
    public string? SpecialInstructions { get; set; }
    public string? InvoiceAdvertiser { get; set; }
    public string? InvoiceCompanyName { get; set; }
    public string? InvoiceAddress1 { get; set; }
    public string? InvoiceAddress2 { get; set; }
    public string? InvoiceAddress3 { get; set; }
    public string? InvoiceCity { get; set; }
    public string? InvoiceStateCounty { get; set; }
    public string? InvoicePostCode { get; set; }
    public string? InvoiceCountryName { get; set; }
    public string? InvoiceContactName { get; set; }
    public string? InvoiceContactEmailAddress { get; set; }
    public List<Item>? Items { get; set; }
}

[PrimaryKey("OrderItemId")]
public class Item
{   
    public int OrderItemId { get; set; }
    public string? ProductName { get; set; }
    public string? PurchaseOrder { get; set; }
    public string? ItemName { get; set; }
    public string? MonthName { get; set; }
    public int Year { get; set; }
    public decimal GrossPrice { get; set; }
    public decimal NetPrice { get; set; }
}

My Index view with a simple form and a table to display the entered data in an EF Core in memory db

<div class="text-center">
    <h1 class="display-4">Invoice Request</h1>
    <p>We require further information.</p>

    <form method="post">

        <div class="row mb-3">
            <div class="col-md-4">
                <input type="text" class="form-control mb-2" name="OrderId" placeholder="Order Id" required="required">
                <input type="text" class="form-control mb-2" name="SalesPerson" placeholder="Sales Person">
                <input type="text" class="form-control mb-2" name="OrderConfirmedDate" placeholder="Order Confirmed Date">
                <input type="text" class="form-control mb-2" name="CurrencyName" placeholder="Currency Name">
                <input type="text" class="form-control mb-2" name="SpecialInstructions" placeholder="Special Instructions">
                <input type="text" class="form-control mb-2" name="InvoiceAdvertiser" placeholder="Invoice Advertiser">
                <input type="text" class="form-control mb-2" name="InvoiceCompanyName" placeholder="Invoice Company Name">
            </div>

            <div class="col-md-4">
                <input type="text" class="form-control mb-2" name="InvoiceAddress1" placeholder="Invoice Address 1">
                <input type="text" class="form-control mb-2" name="InvoiceAddress2" placeholder="Invoice Address 2">
                <input type="text" class="form-control mb-2" name="InvoiceAddress3" placeholder="Invoice Address 3">
                <input type="text" class="form-control mb-2" name="InvoiceCity" placeholder="Invoice City">
                <input type="text" class="form-control mb-2" name="InvoiceStateCounty" placeholder="Invoice State County">
                <input type="text" class="form-control mb-2" name="InvoicePostCode" placeholder="Invoice Post Code">
                <input type="text" class="form-control mb-2" name="InvoiceCountryName" placeholder="Invoice Country Name">
            </div>

            <div class="col-md-4">
                <input type="text" class="form-control mb-2" name="InvoiceContactName" placeholder="Invoice Contact Name">
                <input type="email" class="form-control mb-2" name="InvoiceContactEmailAddress" placeholder="Invoice Contact Email Address">
            </div>
        </div>

        <div class="row mb-3">
            <div class="col-md-12 mb-3">
                <h4>Add/Remove Item to Request</h4>
                <button class="btn btn-secondary" type="button">-</button>
                <button class="btn btn-secondary" type="button">+</button>
            </div>
            <div class="col-md-6">
                <input type="text" class="form-control mb-2" name="Items[0].OrderItemId" placeholder="Order Item Id" required="required">
                <input type="text" class="form-control mb-2" name="Items[0].ProductName" placeholder="Product Name">
                <input type="text" class="form-control mb-2" name="Items[0].PurchaseOrder" placeholder="Purchase Order">
                <input type="text" class="form-control mb-2" name="Items[0].ItemName" placeholder="Item Name">
            </div>

            <div class="col-md-6">
                <input type="text" class="form-control mb-2" name="Items[0].MonthName" placeholder="Month Name, e.g. January">
                <input type="text" class="form-control mb-2" name="Items[0].Year" placeholder="Year">
                <input type="text" class="form-control mb-2" name="Items[0].GrossPrice" placeholder="Gross Price">
                <input type="text" class="form-control mb-2" name="Items[0].NetPrice" placeholder="Net Price">
            </div>
        </div>

        <div class="row">
            <div class="col">
                <button type="submit" class="btn btn-primary">Add Request</button>
            </div>
        </div>
    </form>

    <div class="mt-3">
        <table class="table">
            <thead>

            <tr>
                <th>OrderId</th>
                <th>SalesPerson</th>
                <th>OrderConfirmedDate</th>
                <th>CurrencyName</th>
                <th>SpecialInstructions</th>
                <th>InvoiceAdvertiser</th>
                <th>InvoiceCompanyName</th>
                <th>InvoiceAddress1</th>
                <th>InvoiceAddress2</th>
                <th>InvoiceAddress3</th>
                <th>InvoiceCity</th>
                <th>InvoiceStateCounty</th>
                <th>InvoicePostCode</th>
                <th>InvoiceCountryName</th>
                <th>InvoiceContactName</th>
                <th>InvoiceContactEmailAddress</th>
                <th>Items</th>
            </tr>
            </thead>

            <tbody>
            @foreach (var request in Model.RequestsList)
            {
                <tr>
                    <td>@request.OrderId</td>
                    <td>@request.SalesPerson</td>
                    <td>@request.OrderConfirmedDate</td>
                    <td>@request.CurrencyName</td>
                    <td>@request.SpecialInstructions</td>
                    <td>@request.InvoiceAdvertiser</td>
                    <td>@request.InvoiceCompanyName</td>
                    <td>@request.InvoiceAddress1</td>
                    <td>@request.InvoiceAddress2</td>
                    <td>@request.InvoiceAddress3</td>
                    <td>@request.InvoiceCity</td>
                    <td>@request.InvoiceStateCounty</td>
                    <td>@request.InvoicePostCode</td>
                    <td>@request.InvoiceCountryName</td>
                    <td>@request.InvoiceContactName</td>
                    <td>@request.InvoiceContactEmailAddress</td>
                    <td>
                        @if (@request.Items != null)
                        {
                            @foreach (var item in @request.Items)
                            {
                                <p>@item.OrderItemId</p>
                            }
                        }
                    </td>

                </tr>
            }
            </tbody>
        </table>
    </div>
</div> 

This is my Index.cshtml.cs file that binds the data:

public class IndexModel : PageModel
{
    private readonly RequestDbContext _context;

    public List<Request> RequestsList { get; set; } = [];
    
    [BindProperty] 
    public Request? NewRequest { get; set; }
    
    public IndexModel(RequestDbContext context)
    {
        _context = context;
    }

    public void OnGet()
    {
        RequestsList = _context.Requests.ToList();
    }

    public IActionResult OnPost()
    {
        _context.Requests.Add(NewRequest);
        _context.SaveChanges();
        
        return RedirectToPage();
    }
}

Any help much appreciated

1

Seems like you forgot to add @model in razor page like below.

@page
@model IndexModel

and below add your razor html code.

Thank you, I found the problem. Needed to include the items from the db

RequestsList = _context.Requests.includes(x => x.Items).ToList();

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật