【发布时间】:2021-12-07 08:21:41
【问题描述】:
我第一次使用带有 EF 的 ASP.NET Core MVC 进行 Web 应用开发。我在 SQL Server 数据库中有两个表 Customer 和 Inventory。
我做了Scaffold-DbContext 并为两个表生成了模型/控制器和视图。我的要求是,从网格上每个客户的Customer 索引页面中,我添加了一个选择按钮,当单击此选择按钮时,我需要将选定的客户数据传递给库存控制器并在顶部显示客户选择库存索引页面和下面的我显示带有库存表的网格。
我在这里使用TempData。在客户表上,我添加了选择按钮
<td>
<a asp-action="passToInventory" asp-route-id="@item.CustomerId">Select</a>
</td>
CustomersController:
public async Task<IActionResult> passToInventory(int? id)
{
if (id == null)
{
return NotFound();
}
Customer customer_data = await _context.Customers.FindAsync(id);
TempData["custdata"] = customer_data;
return RedirectToAction("Index", "Inventories");
}
现在在Index 方法中的InventoriesController 上,我进行了更改,例如访问通过的TempData
public async Task<IActionResult> Index()
{
Customer cust_data = TempData["custdata"] as Customer;
return View(await _context.Inventories.ToListAsync());
}
我尝试创建一个模型类,这样我就可以在Inventory Index 页面上同时使用客户和库存列表,如下所示:
public class CustomerInventoryCollectionDataModel
{
public Customer CustomerData { get; set; }
public List<Inventory> Inventorys { get; set; }
}
我无法在库存的索引页面上检索到数据
@model IEnumerable<JAXSurplusMouseApp.Models.CustomerInventoryCollectionDataModel>
//show the selected customer data from the previous page
unable to access the customer data here and show it
//table for the Inventory list
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Inventorys.QuantityAvailable)
</td>
</tr>
}
请建议我如何在同一视图中检索两个模型并将它们显示在同一页面中。非常感谢任何帮助
【问题讨论】:
标签: c# asp.net-mvc entity-framework asp.net-core entity-framework-core