2012-05-29 35 views
0

所以我有这样的代码:如何在ASP.NET MVC3阅读列表的内容在搜索

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

回答

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> 
} 
+0

谢谢,它的工作,但我不得不添加 Danny

+0

@Danny:不客气。很高兴我能帮上忙。 :) – Shyju

+0

您可以通过在视图顶部添加“@using thenamespace.Models”来避免它。 –