ActionResult can only send a single model. If you want to send more than 1 object then use the ViewBag
or a ViewModel
:
ViewBag : These are additional data that you pass to the views in addition to the model. You can send as many as you need and they can be of any type since they are of type Dynamic
:
public ActionResult Entrega(int id)
{
var Productos = db.Productos.Where(d => e.IdEntrega==id).Include(p =>
p.Compra).Include(p => p.Empresa);
ViewBag.Detalles = db.DetalesEntrega.Where( x=> x.EntregaId == id);
return View(Productos.ToList());
}
And to use the values in the view
foreach(DetallesEntrega entrega in ViewBag.Detalles)
{
<div>
@entrega.Fecha
</div>
}
ViewModels : In Spanish: Model of the view. This is a class that exactly represents the view . It contains all the objects that the view requires to work:
public class EntregaViewModel
{
public EntregaViewModel(List<Producto> productos, DetalleEntrega detalle)
{
this.Productos = productos;
this.DetallesEntrega = detalle;
}
public List<Producto> Productos {get; private set;}
public DetalleEntrega Detalle {get; private set;}
}
EntregaViewModel
represents the data that the action results Entrega
requires. Then instead of returning the products we return the viewmodel with the corresponding data:
public ActionResult Entrega(int id)
{
var Productos = db.Productos.Where(d => e.IdEntrega==id).Include(p =>
p.Compra).Include(p => p.Empresa);
var detalles = db.DetalesEntrega.Where( x=> x.EntregaId == id);
EntregaViewModel viewModel = new EntregaViewModel(Productos.ToList(), detalles);
return View(viewModel);
}
Then in your sight:
@model EntregaViewModel
foreach(DetallesEntrega entrega in Model.Detalles)
{
<div>
@entrega.Fecha
</div>
}
foreach(Producto producto in Model.Productos)
{
<div>
@producto .Nombre
</div>
}
Note: I think it would be too much to say that this is just a guide. It is up to you to adapt the example to your needs.