库存、订单项目和订单

发布于 2025-01-11 13:27:47 字数 3459 浏览 0 评论 0原文

我现在面临的问题是我有多个订单,每个订单包含多个项目。每个订单都必须链接到一个客户,每个项目都必须链接到一个库存项目。 这是我的订单类

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.List1[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单 2025-01-18 13:27:47

您的 InvalidOperationException 表示您正在传递 List 但 Razor 页面中的模型是

控制器中的 List 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);
}

您正在访问 _context.Orders,这是您的 Orders 表。您正在.Include-ing OrderItems,但您正在返回您的Orders

var orderItems = ... 传回 OrderItems 或将 Razor 页面的模型调整为 List

如果您想从控制器代码中的 Order 中选择 OrderItems,请将 LINQ 语句更新为:

var orderItems = (await _context.Order
    .Include(s => s.OrderItems)
    .FirstOrDefaultAsync(i => i.OrderItemId == id))
    .Select(x => x.OrderItems); // <-- grab just the OrderItems

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文