【问题标题】:How to read the content of List in a View in ASP.NET MVC3如何在 ASP.NET MVC3 的视图中读取列表的内容
【发布时间】:2012-06-03 07:36:59
【问题描述】:

所以我有这个代码:

public ActionResult Agregar(int id)
        {
            Producto producto = db.Productos.Find(id);

            var list = new List<Carrito> { 
                new Carrito { ProductId = producto.ID, Cantidad = 1, PrecioUnitario = producto.Precio } 
            };

            return View(list);
        }

我将变量list 传递给视图,问题是,我不知道如何访问ProductIdCantidad

知道怎么做吗?

另外,假设变量list有多个对象,如何通过foreach打印每个对象的结果

【问题讨论】:

    标签: c# asp.net-mvc-3 strong-typing


    【解决方案1】:

    你的观点(Agregar.cshtml)应该是这样的。

    @model IEnumerable<Carrito>
    
    <h2>We are gonna show the items in a loop now..</h2>
    @foreach(var item in Model)
    {
      <p>@item.ProductId</p>
      <p>@item.Cantidad </p>
    }
    

    【讨论】:

    • 谢谢,它成功了,但我必须添加
    • 您可以通过在视图顶部添加“@using thenamespace.Models”来避免它。
    猜你喜欢
    • 2013-09-10
    • 1970-01-01
    • 2023-04-03
    • 2020-01-13
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多