0

So I have this code:

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);
        }

and I pass the variable list to the View, the problem is, I don't know how to access ProductId or Cantidad

Any idea how to do this?

Also, let's suppose the variable list has multiple objects, how to print the result of every object through a foreach

4

1 に答える 1

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>
}
于 2012-05-29T04:32:34.613 に答える