Send 2 models to the return of View asp.net

0

In 2 different models I have a method that gives me a list each

Lista<Categorias>------------------Lista<Rutas>

I'm passing one using the following in the ActionResult of the view

public ActionResult Categoria()
{
   return View(ct.listar());
}

How do I send the rt.lista() (the one of the routes), next to the category to the return of View correctly?

    
asked by Baker1562 22.04.2018 в 20:51
source

2 answers

1

I solved it by creating a class with the following data:

public class vmRutasCate
{
    public List<categoria> cate { get; set; }
    public List<rutas> rut { get; set; }

}

and in ActionResult assign the data to an object and send it to View

public ActionResult Categoria()
{
    public vmRutasCate modelEnlace = new vmRutasCate();
    modelEnlace.cate = ct.listar();
    modelEnlace.rut = rt.listarRutas();
    return View(modelEnlace);
}
    
answered by 22.04.2018 / 21:17
source
1

I would use the viewBag instead of doing classes for each time you want to return more than one type of data, the viewBag is used for this, pass dynamic variables.

This is how variables are assigned to viewBag .

public ActionResult Index()
{
    ViewBag.listaCaterogias = miListaCategorias;
    ViewBag.listaRutas= miListaRutas;

    return View();
}

And in the view to collect them

@ViewBag.listaCaterogias 
@ViewBag.listaRutas

ControllerBase.ViewBag Property

    
answered by 23.04.2018 в 13:09