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();