我第一次使用 ASP.NET Core MVC 和 EF 進行 Web 應用程式開發。我在 SQL Server DB 中有兩個表(Customer 和 Inventory 表)我做了 Scaffold-DbContext 并為這兩個表生成了模型/控制器和視圖。我的要求是,從網格上每個客戶的客戶索引頁面添加了一個選擇按鈕,當單擊此選擇按鈕時,我需要將選定的客戶資料傳遞給庫存控制器并在庫存頂部顯示客戶選擇索引頁和下面我顯示了帶有庫存表的網格
我在這里使用 TempData。在客戶表上,我添加了選擇按鈕
<td>
<a asp-action="passToInventory" asp-route-id="@item.CustomerId">Select</a>
</td>
客戶控制器
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 上,我進行了更改,例如訪問傳遞的 TempData
public async Task<IActionResult> Index()
{
Customer cust_data = TempData["custdata"] as Customer;
return View(await _context.Inventories.ToListAsync());
}
我嘗試創建一個模型,以便我可以在庫存索引頁面上同時使用客戶和庫存串列,如下所示
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>
}

請建議我如何在同一視圖中檢索兩個模型并在同一頁面中顯示它們。任何幫助是極大的贊賞
uj5u.com熱心網友回復:
行動
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);
}
or if you need to use TempData customer for some reason, you can use this in the code
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
I don't know maybe you have more code in passToInventory action then it is posted, but the way it looks now IMHO you could go immediately to index
<td>
<a asp-action="Index" asp-controller="Inventories" asp-route-id="@item.CustomerId">Select</a>
</td>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/327181.html
標籤:asp.net asp.net-mvc 实体框架 asp.net核心 实体框架核心
上一篇:不支持連接字串關鍵字“服務器”——在遵循MSDocs上的ASP.NET教程時
下一篇:如何使用LINQ獲取最新號碼
