The problem I'm facing now is I have multiple Orders and each Order contains multiple items. Each Order has to link to a Client and Each item has to link to a Inventory Item. Here's my Order Class
public class Order
{
[Key]
public int Id { get; set; }
public int TotalItems { get; set; }
public DateTime DeliveryDate { get; set; }
public string OrderNumber { get; set; }
public int ClientId { get; set; }
[ForeignKey("ClientId")]
public string DeliveryAddress { get; set; }
public List<OrderItem> OrderItems { get; set; }
public Client Clients { get; set; }
}
OrderItem Class
public class OrderItem
{
public int Id { get; set; }
public int OrderId{ get; set; }
[ForeignKey("OrderId")]
public int InventoryInfoId { get; set; }
[ForeignKey("InventoryInfoId")]
[Required]
public string ItemCode { get; set; }
public int Quantity { get; set; }
public InventoryInfo InventoryInfo { get; set; }
public Order Order { get; set; }
}
Any idea of how I can link them?
I think I have solved the above issues
As soon as I process, the next problem pops out.
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.OrderNumber)
</td>
<td>
@Html.DisplayFor(m=> item.DeliveryAddress)
</td>
<td>
@Convert.ToDateTime(item.DeliveryDate).ToString("dd-MM-yyyy")
</td>
<td>
@Html.DisplayFor(m=>item.Client.ClientCode)
</td>
<td>
@Html.DisplayFor(modelItem => item.TotalItems)
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.Id" >Edit</a>
<a asp-controller="OrderItem" asp-action="OrderDetail" asp-route-id="@item.Id" >Details</a>
<a asp-action="Delete" asp-route-id="@item.Id" >Delete</a>
</td>
</tr>
}
This is my Orders Index page, when the details button is been clicked, the page should redirect to OrderItems page. However it doesnot.
@model List<IOSystem.Models.OrderItem>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.ItemCode)
</td>
<td>
@Html.DisplayFor(m => item.Quantity)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
And here is the error message.
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[IOSystem.Models.Order]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List
1[IOSystem.Models.OrderItem]'.
Forgot to add my controller
[HttpPost, ActionName("OrderDetail")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OrderDetailPost(int? id)
{
if (id == null)
{
return NotFound();
}
var orderItems = await _context.Orders.Include(s=>s.OrderItems).FirstOrDefaultAsync(i => i.OrderItemId == id);
return View(orderItems);
}
CodePudding user response:
Your InvalidOperationException
is saying you're passing a List<Order>
but the model in your Razor page is a List<OrderItem>
In your controller code:
[HttpPost, ActionName("OrderDetail")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> OrderDetailPost(int? id)
{
if (id == null)
{
return NotFound();
}
var orderItems = await _context.Orders.Include(s=>s.OrderItems).FirstOrDefaultAsync(i => i.OrderItemId == id);
return View(orderItems);
}
You're accessing _context.Orders
, which is your Orders
table. You're .Include
-ing the OrderItems
but you're returning your Orders
.
Either pass back the OrderItems
from var orderItems = ...
or adjust your Razor page's Model to be a List<Order>
.
If you want to select the OrderItems
from your Order
in the controller code, update your LINQ statement to:
var orderItems = (await _context.Order
.Include(s => s.OrderItems)
.FirstOrDefaultAsync(i => i.OrderItemId == id))
.Select(x => x.OrderItems); // <-- grab just the OrderItems