首页 > 解决方案 > 使用 EF 框架的 .NET 核心 MVC - 将数据从一个控制器传递到另一个控制器

问题描述

我第一次使用带有 EF 的 ASP.NET Core MVC 进行 Web 应用程序开发。我有两个表CustomerInventory在 SQL Server 数据库中。

我做了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");
}

现在在InventoriesController方法Index中,我进行了更改,例如访问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; }
}

我无法在 Inventory 的 Index 页面上检索到数据

 @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-mvcentity-frameworkasp.net-coreentity-framework-core

解决方案


行动

public async Task<IActionResult> Index()
 {
  Customer custData = TempData["custdata"] as Customer;
  var inventories =await _context.Inventories.ToListAsync();
  var model= new CustomerInventoryCollectionDataModel
{
   CustomerData = custData,
    Inventorys =inventories 
};

  return View(model);
 }

看法

@model JAXSurplusMouseApp.Models.CustomerInventoryCollectionDataModel
  
//show the selected customer data from the previous page 
//  using  @Model.CustData
 
@Html.DisplayFor(model => model.CustData.Name)

....and so on

//table for the Inventory list 
<tbody>
    @foreach (var item in Model.Inventorys)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.QuantityAvailable)
            </td>
         </tr>
     }

但恐怕你需要序列化客户,所以我使用它似乎更有效

public async Task<IActionResult> passToInventory(int? id)
 {
   if (id == null)
   {
     return NotFound();
   }
  
   return RedirectToAction("Index", "Inventories",new { id });

 }

行动

public async Task<IActionResult> Index(int? id)
 {
 if(id==null) return ...error

  Customer custData =   await _context.Customers.FindAsync(id);
  var inventories =await _context.Inventories.ToListAsync();
  var model= new CustomerInventoryCollectionDataModel
{
   CustomerData = custData,
    Inventorys =inventories 
};

  return View(model);
 }

或者如果您出于某种原因需要使用 TempData 客户,您可以在代码中使用它

var customerData = await _context.Customers.FindAsync(id);

TempData["custdata"] =
System.Text.Json.JsonSerializer.Serialize( customerData;)

//index
 Customer custData = System.Text.Json.JsonSerializer.Deserialize<Customer> ( 
TempData["custdata"].ToString());

附言

我不知道您在 passToInventory 操作中是否有更多代码然后发布,但现在看起来像恕我直言,您可以立即索引

<td>
 <a asp-action="Index" asp-controller="Inventories" asp-route-id="@item.CustomerId">Select</a>
</td>

推荐阅读