how to load two models to the same view from a third model that contains both?

0

link first here I leave the project uploaded to github .. second here the code

    // este es el modelo que contiene ambos modelos
    namespace MyFirstWebsite.Models
    {
        public class Modelo
        {
            public List modeloReceta { get; set; }
            public Slide modeloSlide { get; set; }

        public Modelo(List<Receta> modelo1, Slide modelo2)
        {
            modeloReceta = modelo1;
            modeloSlide = modelo2;
        }
       }
}

//luego este es mi controller namespace MyFirstWebsite.Controllers { public class HomeController : Controller { private RepositorioRecetas _reposRecetas = null; private Modelo _modelos = null; public List modeloReceta = null; public Slide modeloSlide = null;

        public HomeController ()         {         }         [HttpPost]         public ActionResult Index ()         {             if (ModelState.IsValid)             {                 return RedirectToAction ("Index");             }             return View (_models);         }         public ActionResult About ()         {             ViewBag.Message="Your application description page.";             return View ();         }         public ActionResult Contact ()         {             ViewBag.Message="Your contact page.";             return View ();         }         public ActionResult Recipes (int? id)         {             if (id == null)             {                 return HttpNotFound ();             }             var recipe = _reposRecetas.GetReceta ((int) id);             return View (recipe);         }     } }

one of the models has a repository of recipes in the other model is to return dynamic to a slider ...

    
asked by CeciPeque 10.07.2018 в 16:50
source

1 answer

0

Make your view accept the model type MyFirstWebsite.Models.Modelo and change your controller for the following:

public ActionResult Recetas(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }
            var slide = new Slide{/*...*/};
            var receta = _reposRecetas.GetReceta((int)id);

            var modelo = new Modelo(receta, slide);
            return View(modelo);
        }

Or you can also use the MVC ViewBag object:

public ActionResult Recetas(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            ViewBag.Slide = new Slide{/*...*/};
            var receta = _reposRecetas.GetReceta((int)id);

            return View(receta);
        }

And you access it from your view as @ViewBag.Slide

You can find more information about the ViewBag object in link

Greetings.

    
answered by 10.07.2018 в 23:35