库存、订单项目和订单
我现在面临的问题是我有多个订单,每个订单包含多个项目。每个订单都必须链接到一个客户,每个项目都必须链接到一个库存项目。 这是我的订单类
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 类
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; }
}
知道如何链接它们吗?
我想我已经解决了上述问题
一旦我处理,下一个问题就会出现。
@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" class="btn btn-success">Edit</a>
<a asp-controller="OrderItem" asp-action="OrderDetail" asp-route-id="@item.Id" class="btn btn-success">Details</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-success">Delete</a>
</td>
</tr>
}
这是我的订单索引页面,当单击详细信息按钮时,该页面应重定向到 OrderItems 页面。然而事实并非如此。
@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>
}
这是错误消息。
InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“System.Collections.Generic.List1[IOSystem.Models.Order]”,但此 ViewDataDictionary 实例需要类型为“System.Collections.Generic”的模型项。列表
1[IOSystem.Models.OrderItem]'。
忘记添加我的控制器
[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);
}
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" class="btn btn-success">Edit</a>
<a asp-controller="OrderItem" asp-action="OrderDetail" asp-route-id="@item.Id" class="btn btn-success">Details</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-success">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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
InvalidOperationException
表示您正在传递List
但 Razor 页面中的模型是控制器中的
List
code:您正在访问
_context.Orders
,这是您的Orders
表。您正在.Include
-ingOrderItems
,但您正在返回您的Orders
。从
var orderItems = ...
传回OrderItems
或将 Razor 页面的模型调整为List
。如果您想从控制器代码中的
Order
中选择OrderItems
,请将 LINQ 语句更新为:Your
InvalidOperationException
is saying you're passing aList<Order>
but the model in your Razor page is aList<OrderItem>
In your controller code:
You're accessing
_context.Orders
, which is yourOrders
table. You're.Include
-ing theOrderItems
but you're returning yourOrders
.Either pass back the
OrderItems
fromvar orderItems = ...
or adjust your Razor page's Model to be aList<Order>
.If you want to select the
OrderItems
from yourOrder
in the controller code, update your LINQ statement to: