Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Two repositories for one view

I have a ViewModel that contains all of my Models, both with it's own repository and I'm trying to put them toguether in the same view... this is in Controller

public ActionResult Index()
        {
            var id = new Receta().Id;

            var viewModel = new Modelos()
            {
                Receta = new Receta(),
                Slide = new Slide(),
                GetRecetas = new Receta[id]

            };

            return View(viewModel);
        }

this is in ViewModel

public class Modelos
    {
        public Receta Receta { get; set; }
        public Slide Slide { get; set; }
        public Receta[] GetRecetas { get; set; }
    }

and this is one of the repositories

public class RepositorioRecetas
    {
        private static Receta[] _recetas = new Receta[]
        {
            new Receta()
            {
                Id = 1,
                Titulo = "Receta1",
                DescripcionHtml = "<p>corta descripcion del producto</p>",
                Favorito = false,
                RecetaPasos = "bla bla bla"
            },

            new Receta()
            {
                Id = 2,
                Titulo = "Receta2",
                DescripcionHtml = "<p>corta descripcion del producto</p>",
                Favorito = false,
                RecetaPasos = "bla bla bla"
            },

            new Receta()
            {
                Id = 3,
                Titulo = "Receta3",
                DescripcionHtml = "<p>corta descripcion del producto</p>",
                Favorito = false,
                RecetaPasos = "bla bla bla"
            }
         };

        public Receta[] GetRecetas()
        {
            return _recetas;
        }

        public Receta GetReceta(int id)
        {
            Receta recetaReturn = null;

            foreach (var receta in _recetas)
            {
                if(receta.Id == id)
                {
                    recetaReturn = receta;
                    break;
                }
            }

            return recetaReturn;
        }
    }
Steven Parker
Steven Parker
216,136 Points

And did you have a question about this?

yes but here I show more code, I have a nullReferenceException but I do set the id so I don't get why I'm not being able to loop through the foreach

@if(Model.Recetas != null)
{
<div class="row">
    @foreach (Receta receta in Model.Recetas)
    {
        <div class="col-md-4">
            <div class="well">
                <h4>@Html.ActionLink(receta.Titulo, "Receta", new { id = receta.Id, @class = "Recetas" })</h4>
                <a href="@Url.Action("Receta", new { id = receta.Id })">
                    <img src="~/img/fotos/@receta.CoverImageFileName" alt="@receta.MostrarTexto" class="img-responsive" />
                </a>
            </div>
        </div>
    }
</div>
}

3 Answers

please help

Steven Parker
Steven Parker
216,136 Points

I don't quite understand what you're intending from the code, but when you create the viewmodel in the controller, perhaps the array is being filled with nulls instead of being empty (length 0)? Try setting a breakpoint at the controller's return and examine the viewmodel at that point.

hey I figured out how, the code in my controller was wrong and also in the for loop but now everything is working and thanks for the awnser :)