使用 EF Framework 的 .NET 核心 MVC - 将数据从一个控制器传递到另一个
.NET core MVC that uses the EF Framework - Pass the data from one controller another
我是第一次使用 ASP.NET 核心 MVC 和 EF 进行 Web 应用程序开发。我在 SQL 服务器数据库中有两个 tables Customer
和 Inventory
。
我做了 Scaffold-DbContext
并为 table 生成了 models/controllers 和视图。我的要求是,从网格上每个客户的 Customer
索引页面我添加了一个 select 按钮,单击此 select 按钮时我需要传递 select将客户数据输入库存控制器,并在库存索引页面顶部显示客户 select,在下方我显示带有库存的网格 table。
我这里用的是TempData
。在客户 table 我添加了 select 按钮
<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
passed
public async Task<IActionResult> Index()
{
Customer cust_data = TempData["custdata"] as Customer;
return View(await _context.Inventories.ToListAsync());
}
我尝试创建一个模型 class,这样我就可以在 Inventory
Index
页面上同时使用客户和库存列表,如下所示:
public class CustomerInventoryCollectionDataModel
{
public Customer CustomerData { get; set; }
public List<Inventory> Inventorys { get; set; }
}
我无法在 Inventory 的索引页面上检索到数据
@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>
}
请建议我如何在同一视图中检索这两个模型并将它们显示在同一页面中。非常感谢任何帮助
动作
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>
}
但恐怕你需要序列化 customer ,所以我用这个似乎更有效率
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());
PS
我不知道也许你在 passToInventory 操作中有更多的代码然后它被发布了,但它现在看起来的方式恕我直言你可以立即去索引
<td>
<a asp-action="Index" asp-controller="Inventories" asp-route-id="@item.CustomerId">Select</a>
</td>
我是第一次使用 ASP.NET 核心 MVC 和 EF 进行 Web 应用程序开发。我在 SQL 服务器数据库中有两个 tables Customer
和 Inventory
。
我做了 Scaffold-DbContext
并为 table 生成了 models/controllers 和视图。我的要求是,从网格上每个客户的 Customer
索引页面我添加了一个 select 按钮,单击此 select 按钮时我需要传递 select将客户数据输入库存控制器,并在库存索引页面顶部显示客户 select,在下方我显示带有库存的网格 table。
我这里用的是TempData
。在客户 table 我添加了 select 按钮
<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
passed
public async Task<IActionResult> Index()
{
Customer cust_data = TempData["custdata"] as Customer;
return View(await _context.Inventories.ToListAsync());
}
我尝试创建一个模型 class,这样我就可以在 Inventory
Index
页面上同时使用客户和库存列表,如下所示:
public class CustomerInventoryCollectionDataModel
{
public Customer CustomerData { get; set; }
public List<Inventory> Inventorys { get; set; }
}
我无法在 Inventory 的索引页面上检索到数据
@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>
}
请建议我如何在同一视图中检索这两个模型并将它们显示在同一页面中。非常感谢任何帮助
动作
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>
}
但恐怕你需要序列化 customer ,所以我用这个似乎更有效率
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());
PS
我不知道也许你在 passToInventory 操作中有更多的代码然后它被发布了,但它现在看起来的方式恕我直言你可以立即去索引
<td>
<a asp-action="Index" asp-controller="Inventories" asp-route-id="@item.CustomerId">Select</a>
</td>