Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,248 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
231,248 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 :)