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#

in razor I have @model MyFirstWebsite.Models.Modelo and I want to achieve @model MyFirstWebsite.Models.Modelo.Receta

Modelo is a container for my other "child" models but in razor how can I do ".Receta" if visual studio doesn't give me the option.. the only code I have in Modelo is: '''C# namespace MyFirstWebsite.Models { public class Modelo { public List<Receta> Receta { get; set; } public List<Slide> Slide { get; set; } } } '''

2 Answers

yes I was mising an using directive I have

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

thanks a lot

I have another problem now when I pass "Model.Recetal" in my foreach loop I pass null but I sopose to pass an id, this causes an exeption.. how can avoid this?

@model MyFirstWebsite.ViewModel.Modelos 
<div class="row">
    @foreach (var receta in Model.Recetas)
    {
        <div class="col-md-4">
            <div class="well">
                <h4>@Html.ActionLink(receta.MostrarTexto, "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>
Steven Parker
Steven Parker
231,248 Points

From the system's point of view, "Receta" isn't a model but a property of the "Modelo" model. So if you pass that model to your razor page, you should be able to access the property with dot notation: "@Model.Receta".

Steven Parker
Steven Parker
231,248 Points

To the person who down-voted this answer... :-1:

I'd appreciate your feedback about why you found this answer unacceptable, and what I could do to make it better.