【问题标题】:Error in passing data from the view to the Controller将数据从视图传递到控制器时出错
【发布时间】: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&lt;JAXSurplusMouseApp.Models.Item&gt;' 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


    【解决方案1】:

    你必须修正你的观点

    @if ( @ViewBag.cart != null && ViewBag.cart.Count>0)
    {
    <a asp-action="Index" asp-controller="Inventories" asp-route-id=" @ViewBag.cart[0].Custom.CustomerId">Select</a>
    }
    ````
    or  place ancor inside of foreach
    ````
     @foreach (var item in ViewBag.cart)
        {
            <tr>
                <td>@item.Inventory.StrainId</td>
                <td>@item.Inventory.StrainName</td>
                <td>@item.Quantity</td>
                 <td>
    <a asp-action="Index" asp-controller="Inventories" asp-route-id="@item.Custom.CustomerId">Select</a>
                </td>
            </tr>
        }
     </table>
     <br>
    

    【讨论】:

    • 不,我希望锚点在桌子外面。允许用户向后导航
    • 那么你将不得不发布整个代码。如果绝对不可能弄清楚您发布的所有图片代码来自哪里。获取操作和获取视图在哪里,发布操作和发布视图在哪里
    • 非常感谢它现在可以工作,但是if ( @ViewBag.cart != null &amp;&amp; ViewBag.cart.Count&gt;0) 如何在视图中处理这个问题。因为这个 If 语句出现在屏幕上
    • 打错了,应该是@If
    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多