首页 > 解决方案 > 当我在 EF 中使用多对多关系时,我应该如何在视图中显示配方中的所有成分?

问题描述

所以我想在视图中显示一个食谱的所有成分。我用 EF 创建了一个成分表和一个食谱表。关系是多对多的。我不知道如何在一个视图中显示食谱的成分。

我尝试修改模型以便显示成分,但我只能看到一种成分。

这是我的成分类:

namespace Licenta.Models
{
    public class Ingredient
    {
        [Key]
        public int IDIngredient { get; set; }
        public string Nume { get; set; }
        public int Kcal { get; set; }
        public int Pro { get; set; }
        public int Carbo { get; set; }
        public int Fat { get; set; }
        public IList<Recipe> Recipes { get; set; }
    }
}

这是我的食谱课:

namespace Licenta.Models
{
    public class Recipe
    {
        [Key]
        public int IDRecipe { get; set; }
        public string Name { get; set; }
        public string Desc { get; set; }
        public string Steps { get; set; }
        public int Kcal { get; set; }
        public int Pro { get; set; }
        public int Carbo { get; set; }
        public int Fat { get; set; }
        public IList<Ingredient> Ingredients { get; set; }

    }
}

这是我的观点:

@model Licenta.Models.Recipe

@{
    ViewBag.Title = "Details";
}

<h2>Recipe Details</h2>

<div>
    <h4>Recipe</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            Name:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>
        <dt>
            Calories:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Kcal) calorii
        </dd>
        <dt>
            Proteins:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Pro) grame
        </dd>
        <dt>
            Carbs:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Carbo) grame
        </dd>
        <dt>
            Fat:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Fat) grame
        </dd>
        <dt>
            Description:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Desc)
        </dd>
        <dt>
            Steps:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Steps)
        </dd>
        <dt>
            Ingredients:
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Ingredients)
        </dd>
    </dl>
</div>

我正在使用的控制器是这个:

namespace Licenta.Controllers
{
    public class HomeController : Controller
    {

        private DataContext db = new DataContext();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Recipes()
        {
            return View(db.Recipes.ToList());
        }

        public ActionResult Details(int? id)
        {
            if(id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Recipe recipe = db.Recipes.Find(id);

            if(recipe == null)
            {
                return HttpNotFound();
            }

            return View(recipe);
        }

    }
}

我还看到 EF 在我的数据库中创建了一个 RecipesIngredients 表。我应该如何使用该表列出我的食谱的成分?

标签: c#asp.net-mvcentity-framework

解决方案


从数据库中查询配方时,您需要包括配方的成分。

即:替换

db.Recipes.Find(id);

db.Recipes.Include(x => x.Ingredients).Single(y => y.Id == id);

要显示成分详细信息,您需要遍历成分并显示您想要的每种成分的任何数据。

即:替换

 <dd>
     @Html.DisplayFor(model => model.Ingredients)
 </dd>

@foreach(var ingredient in Model.Ingredients)
{
     /*feel free to format/display this however you want.
       this is only intended to show you how to accomplish
       what you're asking*/

     Nume: @ingredient.Nume
     Carbo: @ingredient.Carbo
     Pro: @ingredient.Pro
}

您还可以创建一个以 aIList<Ingredient>作为模型的显示模板。如果您这样做了,则根本不需要更改当前模型,因为DisplayFor会自动查找要使用的显示模板。


推荐阅读