子foreach不使用ViewModel在Partial中按预期循环

时间:2013-12-08 22:19:43

标签: c# asp.net-mvc foreach viewmodel partials

我有食谱列表,每个食谱都有许多食谱,我想循环使用它的相应配方行foreach循环在父食谱foreach内。

食谱类

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

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

RecipeLine Class

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

            public int MeasurementID { 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; }

        }
    }

无需列出成分和测量等级,但结构良好。

现在让我们看一下ViewModel

    namespace XXX.ViewModels
    {
        public class RecipeLineViewModel
        {
            public IEnumerable<Recipe> Recipes { get; set; }
            public IEnumerable<RecipeLine> RecipeLines { get; set; }
        }
    }

和PartialsController

    namespace XXX.Controllers
    {

        public class PartialsController : Controller
        {
            private XXXDb db = new XXXDb();

            public ActionResult RecipeList()
            {
                RecipeLineViewModel viewModel;

                viewModel = new RecipeLineViewModel();
                viewModel.Recipes = db.Recipes.ToList();
                viewModel.RecipeLines = db.RecipeLines.Include(r => 
                r.Measurement).Include(r => r.Ingredient);

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

现在部分视图是视图&gt;部分&gt; __RecipeList.cshtml

RecipeList部分视图

    @model XXX.ViewModels.RecipeLineViewModel

    @foreach (Recipe recipe in Model.Recipes) 
        {
        <div>@recipe.RecipeName</div>
        <div>@recipe.RecipeInstructions</div>

            foreach (RecipeLine recipeLines in Model.RecipeLines)
            {
                <div class="row">
                    <div class="large-12 columns">
                        @recipeLines.Quantity @recipeLines.Measurement.MeasurementEn 
                        @recipeLines.Ingredient.IngredientNameEn
                    </div>
                </div>
            }

        }
    }

(我在每个recipeLine的末尾打印了RecipeID,以显示每个食谱重复RecipeID的配方行= 1而不是他们的行与他们自己的RecipeID相关。我是这个东西的新手,我是假设某个地方我应该告诉Recipe Lines关于RecipeID他们应该循环????

以下是我得到的结果:

Parent/Child Loop Result:

2 个答案:

答案 0 :(得分:2)

Recipie模型中,您拥有List<RecipieLine>属性,可以假设该属性是维护一个或多个RecipieLine模型对象。在RecipieLineViewModel {} {}}中,您有两个单独的IEnumerable个集合,展示了您的RecipieRecipieLine个对象。

在您的实际视图中,您正在迭代Recipie集合:

@foreach (Recipe recipe in Model.Recipes)

看起来不错,但是,以下行是您的问题:

foreach (RecipeLine recipeLines in Model.RecipeLines)

这列举了RecipieLine RecipieLineViewModel集合中存储的RecipieLine模型对象,但是,它目前无法知道您当前列举的Recipieforeachwhere

您想要使用当前Recipie对象枚举中可用的数据提供某种foreach LINQ子句,或者更改内部Recipie以使用当前{ {1}}枚举而不是Model

<强>更新

为了完整性,以下是如何使用当前Recipe对象枚举来访问其关联的RecipieLine的示例:

@forech (var recipie in Model.Recipies)
{
    <div>@recipe.RecipeName</div>
    <div>@recipe.RecipeInstructions</div>
    foreach (var recipieLine in recipie.RecipieLines)
    {
        <div class="row">
            <div class="large-12 columns">
                // Do something with recipieLine here
            </div>
        </div>
    }
}

以上是我首选的方法,因为每次枚举执行LINQ查询更有效,并从IEnumerable<RecipieLine>删除ViewModel,这实际上是重复的代码。

答案 1 :(得分:1)

在foreach中添加一个Linq子句。

   @foreach (Recipe recipe in Model.Recipes) 
            {
            <div>@recipe.RecipeName</div>
            <div>@recipe.RecipeInstructions</div>

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

UPD:
此外,您可以在Recipes中明确包含RecipeLine内容,您根本不需要RecipeLine Collection!只需循环通过recipe.RecipeLines

这样的事情:

viewModel.Recipes = db.Recipes.Include("RecipeLine").Include("RecipeLine.Measurement").
                .Include("RecipeLine.Ingredient");