Display data from 1 table and its relationship with another MVC

0

Good day mates I have this Action Result in the controller Products that looks for all the products of a certain IdEntrega and I send it to call from the Delivery view to know all the products of that delivery and it works well but now I need that besides appearing all the products of that delivery in the view, also some data of the delivery in the top of the view, how can I make it?

public ActionResult Entrega(int id) 
{ 
var Productos = db.Productos.Where(d => e.IdEntrega==id).Include(p => 
p.Compra).Include(p => p.Empresa); 
return View(Productos.ToList()); 
} 

@Html.ActionLink("Detalles", "Entrega", "Productos", new { id = item.IdEntrega }, null)
    
asked by senseilex 28.08.2017 в 16:20
source

2 answers

0

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.

    
answered by 28.08.2017 / 17:14
source
1

You can create a model that has both models.

public class MiModelo
{
    public List<Producto> Productos{ get; set; }
    public DetallesEntrega DetallesEntrega { get; set; }
}

And in the controller you simply fill it in.

public ActionResult Entrega(int id) 
{ 
var Productos = db.Productos.Where(d => e.IdEntrega==id).Include(p => p.Compra).Include(p => p.Empresa); 

var miModelo = new MiModelo();
miModelo.Productos = Productos.ToList();
miModelo.DetallesEntrega = new DetallesEntrega();

return View(miModelo); 
} 

Do not forget to declare MyModel in the view.

    
answered by 28.08.2017 в 17:55