Question

I have a Model where Recipes have corresponding RecipeLines, I am trying to print out a list of those Recipes looping through the RecipeLines that have a corresponding RecipeID for that particular Recipe. I'm almost there...

Here are the Models

Models/Recipe.cs

namespace XXX.Models
{
    public class Recipe
    {
        public int RecipeID { get; set; }
        public string RecipeName { get; set; }
        public string RecipeInstructions { get; set; }
        public int Serves { get; set; }
        public float PrepTime { get; set; }

        public virtual List<RecipeLine> RecipeLines { get; set; }
    }
}

Models/RecipeLine.cs

namespace XXX.Models
{
    public class RecipeLine
    {
        public int RecipeLineID { get; set; }
        public string Quantity { get; set; }
        public float MeasureAmount { get; set; }
        public int MeasurementID { get; set; }
        public string Instruction { get; set; }

        public int RecipeID { get; set; }
        public int IngredientID { get; set; }

        public virtual Measurement Measurement { get; set; }
        public virtual Recipe Recipe { get; set; }
        public virtual Ingredient Ingredient { get; set; }
    }
}

Models/Measurement.cs

namespace XXX.Models
{
    public class Measurement
    {
        public int MeasurementID { get; set; }
        public string Measurement { get; set; }

        public virtual List<RecipeLine> RecipeLines { get; set; }
    }
}

Models/Ingredient.cs

namespace XXX.Models
{
    public class Ingredient
    {
        public int IngredientID { get; set; }
        public string IngredientName { get; set; }
        public string IngredientDescript { get; set; }

        public virtual List<RecipeLine> RecipeLines { get; set; }
    }
}

Here is the ViewModel

ViewModels/RecipeLineViewModel.cs

namespace XXX.ViewModels
{
    public class RecipeLineViewModel
    {
        public IEnumerable<Recipe> AllRecipes { get; set; }
        //public IEnumerable<RecipeLine> AllRecipeLines { get; set; }

        private ApplicationDbContext db = new ApplicationDbContext();

        public void PopulateModel()
        {
            AllRecipes = db.Recipes.Include("RecipeLines")
           .Include("RecipeLines.Measurement")
           .Include("RecipeLines.Ingredient");
        }
    }
}

My Partial Controller

Controllers/Partial.cs

using XXX.ViewModels;
using XXX.Models;

namespace XXX.Controllers
{
    public class PartialsController : Controller
    {
        public ActionResult RecipeList()
        {
            RecipeLineViewModel model = new RecipeLineViewModel();
            model.PopulateModel();

            return PartialView("_RecipeList", model);
        }
    }
}

OK, Now for the PartialView that is giving me problems during the NESTED FOR LOOP. The FIRST FOR LOOP is working fine. It's the NESTED LOOP that I obviously have wrong

Partials/_RecipeList.cshtml

@model XXX.ViewModels.RecipeLineViewModel
<div class="row">
    <div class="large-6 columns">
        <h4>Recipes&nbsp;&nbsp;&nbsp;&nbsp;@if (Request.IsAuthenticated && User.IsInRole("admin"))
         { @Html.ActionLink("Create New", "Create", "Recipe") }</h4>
        <p>Our recipes are a combination of meals we have put together using our own products and meals our customers have submitted of their own creation using our products!</p>
    </div>
    <div class="large-2 columns">&nbsp;&nbsp;</div>@*Blank column*@
    <div class="large-4 columns">
        <h4>Select a Recipe</h4>
        @foreach (var recipe in Model.AllRecipes)
        {
            <a href="#@recipe.RecipeName" class="small button">@recipe.RecipeName</a>
        }
    </div>
</div>

@foreach (var recipe in Model.AllRecipes)
{
    <div class="row" id="@recipe.RecipeName"><div class="large-12 columns"><hr /></div></div>

    <div class="row">
        <div class="large-4 columns">
            @if (File.Exists(Server.MapPath("~/Images/recipes/" + recipe.RecipeImage)))
            { <img src="~/Images/recipes/@recipe.RecipeImage"> }
            else
            { <h5>No image available</h5> }
            <br /><br />
            @if (Request.IsAuthenticated && User.IsInRole("admin"))
            {
                <div class="row"><div class="large-12 columns"><strong>UPDATE RECIPE: </strong> @Html.ActionLink("Edit", "Edit", "Recipe", new { id = recipe.RecipeID }, null) | @Html.ActionLink("Details", "Details", "Recipe", new { id = recipe.RecipeID }, null) | @Html.ActionLink("Delete", "Delete", "Recipe", new { id = recipe.RecipeID }, null)</div></div>
            }
        </div>

        <div class="large-8 columns">
            <h4>@recipe.RecipeName / @recipe.RecipeName</h4>
            <div>
                Prep Time: @recipe.PrepTime Mins / Serves: @recipe.Serves People
                @if (Request.IsAuthenticated && User.IsInRole("admin"))
                { @Html.ActionLink("Edit", "Edit", "Recipe", null, null, "Serves", new { id = recipe.RecipeID }, null) }

            </div>
            <br /><strong>Ingredients:</strong><br /><br />
            @foreach (RecipeLine recipeLines in Model.RecipeLines.Where(rl => rl.RecipeID == recipe.RecipeID))
            {
                <div>
                    @if (recipeLines.Quantity != "0")
                    {
                        @recipeLines.Quantity }
                    @if (recipeLines.MeasureAmount != 0)
                    {
                        @recipeLines.MeasureAmount }
                    @if (recipeLines.Measurement.Measurement != "none")
                    {
                        @recipeLines.Measurement.Measurement }
                    @recipeLines.Ingredient.IngredientName @recipeLines.Instruction
                    @if (Request.IsAuthenticated && User.IsInRole("admin"))
                    {
                        @Html.ActionLink("Edit", "Edit", "RecipeLines", new { id = recipeLines.RecipeLineID }, null);
                    }
                    @if (Request.IsAuthenticated && User.IsInRole("admin"))
                    {
                        @Html.ActionLink("Delete", "Delete", "RecipeLines", new { id = recipeLines.RecipeLineID }, null) }
                </div>
            }
            @if (Request.IsAuthenticated && User.IsInRole("admin"))
            { @Html.ActionLink("+", "Create", "RecipeLines", new { recipeid = recipe.RecipeID }, null);
            }
            <br />

        </div>
    </div>

    <div class="row">
        <div class="large-12 columns">
            <strong>Instructions:</strong>
            @if (Request.IsAuthenticated && User.IsInRole("admin"))
            {
                @Html.ActionLink("Edit", "Edit", "Recipe", null, null, "Instructions", new { id = recipe.RecipeID }, null);
            }
            <br /><br />
            <div>@Html.Raw(recipe.RecipeInstructions.Replace("\n", "<br />"))</div><br />
        </div>
    </div>
}

If that is too much code for you to look at Here is a shortened version that get's the same point across..

Abbreviated Code for Partials/_RecipeList.cshtml

@model XXX.ViewModels.RecipeLineViewModel
@foreach (var recipe in Model.AllRecipes)
{
@recipe.RecipeName

    @foreach (var recipeLines in Model.AllRecipes.RecipeLines.Where(rl => rl.RecipeID == recipe.RecipeID))
    {
    @recipeLines.Measurement.Measurement
    @recipeLines.Ingredient.IngredientName
    @recipeLines.Instruction
    }
}

Yes I am a total noob when it comes to this type of operation. I would like to understand it better, but I have been struggling to figure it out. Any help would be appreciated. I know I'm almost there.

Here is the current error:

enter image description here

Was it helpful?

Solution

In your code you have this...

@foreach (RecipeLine recipeLines in Model.RecipeLines.Where(rl => rl.RecipeID == recipe.RecipeID))
        {
...

The offending line specified by the error Model Contains no definition for RecipeLines is this...

Model.RecipeLines.Where(rl => rl.RecipeID == recipe.RecipeID)

You need to change that to this

recipe.RecipeLines.Where(rl => rl.RecipeID == recipe.RecipeID)

recipe comes from the outer loop, and RecipeLines is coming from your Recipe class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top