【发布时间】:2021-12-08 14:52:10
【问题描述】:
我在视图页面上有一个按钮,它通过传递一个 ID 值连接到另一个控制器。
控制器
public class CartController : Controller
{
......
[Route("index")]
public IActionResult Index()
{
var cart = SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart");
ViewBag.cart = cart;
return View();
}
[Route("buy/{customerID}/{invetoryID}")]
public async Task<IActionResult> Buy(int? customerID, int? invetoryID)
{
if (customerID == null || invetoryID == null)
{
return NotFound();
}
Customer custData = await _context.Customers.FindAsync(customerID);
var intData = await _context.Inventories.FindAsync(invetoryID);
if (SessionHelper.GetObjectFromJson<List<Item>>(HttpContext.Session, "cart") == null)
{
List<Item> cart = new List<Item>();
cart.Add(new Item
{
Custom = custData,
Inventory = intData,
Quantity = 1
});
SessionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart);
}
return RedirectToAction("Index");
} }}
型号
public class Item
{
public Customer Custom { get; set; }
public Inventory Inventory { get; set; }
public int Quantity { get; set; }
}
如下图所示
<table cellpadding="2" cellspacing="2" border="1">
.......
@{var dataCart = ViewBag.cart; }
@foreach (var item in ViewBag.cart)
{
<tr>
<td>@item.Inventory.StrainId</td>
<td>@item.Inventory.StrainName</td>
<td>@item.Quantity</td>
</tr>
}
</table>
<br>
<a asp-action="Index" asp-controller="Inventories" asp-route-id="@dataCart.Custom.CustomerId">Select</a>
</body>
</html>
当我加载页面时它会抛出错误,它会抛出像RuntimeBinderException: 'System.Collections.Generic.List<JAXSurplusMouseApp.Models.Item>' does not contain a definition for 'Custom'这样的错误
即使 Item 模型和 ViewBag 具有 Custom,它也不会抛出任何定义。谁能告诉我在这里想念什么。如果我没有按钮,那么页面加载非常好。
<a asp-action="Index" asp-controller="Inventories" asp-route-id="@dataCart.Custom.CustomerId">Select</a>
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-core .net-core