部分视图中的嵌套For循环不能使用MVC5 ViewModel

时间:2014-02-16 03:44:59

标签: asp.net for-loop asp.net-mvc-5 asp.net-mvc-partialview asp.net-mvc-viewmodel

我有一个模型,其中食谱有相应的RecipeLines,我试图打印出一个循环通过RecipeLines的食谱的列表,其具有该特定食谱的相应RecipeID。我快到了......

以下是模型

模型/ 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; }
    }
}

模型/ 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; }
    }
}

模型/ 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; }
    }
}

模型/ 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; }
    }
}

这是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");
        }
    }
}

我的部分控制器

控制器/ 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);
        }
    }
}

好的,现在是在NESTED FOR LOOP期间给我带来问题的PartialView。 FIRST FOR LOOP工作正常。这是NESTED LOOP,我显然错了

局部模板/ _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>
}

如果这是太多的代码供你查看这里有一个缩短的版本,可以得到相同的点...

缩写代码 的局部模板/ _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
    }
}

是的,对于这种类型的操作,我是一个完全的菜鸟。我想更好地理解它,但我一直在努力弄明白。任何帮助,将不胜感激。我知道我快到了。

以下是当前错误:

enter image description here

1 个答案:

答案 0 :(得分:1)

在你的代码中你有这个......

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

错误Model Contains no definition for RecipeLines指定的违规行是......

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

您需要将其更改为此

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

recipe来自外部循环,而RecipeLines来自您的Recipe