简体   繁体   中英

How do you reference a foreign key from a razor view page?

New to .net core here and I am going through razor tutorials and trying to add my own things to it. I have two tables: Order and Stock. I can list all Orders or all stocks, but when I want to list all Orders, I want to display the stock name (not the stockID) in the view page (near end of code). How do you reference the foreign key 'stockID' from the view page? Here is my code:

public class OrderModel
{
    public int Id { get; set; }
    public string OrderName { get; set; }
    public DateTime OrderDate { get; set; } = DateTime.UtcNow;
    public int Quantity { get; set; }
    public decimal StockUnitCost { get; set; }
    public decimal StockTotalCost { get; set; }
    public decimal WinLoseAmt { get; set; }
    public int StockId { get; set; }

}

 public class StockModel
{
    public int Id { get; set; }
    public string StockName { get; set; }
    public string StockTicker { get; set; }
    public string StockType { get; set; }
    public string Sector { get; set; }
}
     public class OrderDisplayModel : PageModel
    {
        private readonly IOrderData _orderData;
        private readonly IStockData _stockData;

        public OrderModel Order { get; set; }
        public List<OrderModel> orderList { get; set; }

        public OrderDisplayModel(IOrderData orderData, IStockData stockData)
        {
            _orderData = orderData;
            _stockData = stockData;
        }

        public async Task OnGet()
        {
            
            orderList = await _orderData.GetOrder();
        }

    }
    <h1>My Orders</h1>

<table class="'table table-striped">
    <thead class="thead-dark">
        <tr>
            <th>Order Name</th>
            <th>Stock</th>
            <th>Quantity</th>
            <th>Cost Basis</th>
            <th>Total Cost</th>
            <th>Open Mkt Gain</th>
            <th>Order Date</th>
        </tr>
    </thead>
    <tbody>
        

        @foreach (var order in Model.orderList)
        {
           
        <tr>
            <td>@order.OrderName</td>
            <td>@Model.StockPurchased</td>
            <td>@order.StockId</td>
            <td>@order.Quantity</td>
            <td>@order.StockUnitCost</td>
            <td>@order.StockTotalCost</td>
            <td>@order.WinLoseAmt</td>
            <td>@order.OrderDate</td>
        </tr>
        }
    </tbody>
</table>

Thanks!

Create a virtual property and make it ForeignKey (and InverseProperty if you want to have another virtual collection property from Stock to Orders). Something like this

....
public int StockId { get; set; }
[ForeignKey(nameof(StockId))]
[InverseProperty("Orders")]
public virtual Stock Stock { get; set; }

Then, you can either .Include(s=>s.Stock) in you context call and access it like order.Stock.StockName. Or setup Lazy loading (which requires aa bit more work in recent .NET core versions) and you access it without needing to Inclue it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM