【发布时间】:2017-02-24 12:23:07
【问题描述】:
您好,我是一名初学者,我正在使用 ASP.NET 和 MVC5。
我想在 1 个 foreach 中创建一个带有嵌套标题(课程)的 IEnumerable Food-Menu-Pricelist,这样我就可以使用 CRUD-Operations I 创建或编辑一个新字符串(CourseName)用实体框架做的。
我将在下面的链接中嵌入一张图片,它是我进行半/硬编码的,就像您在下面的代码中看到的那样标题是硬编码的。
我想要达到的结果预览:
我有 2 个模型:
- Course.cs = 带有属性字符串的标题:CourseName
- Dish.cs = 以及带有属性字符串的列表:DishName
有没有一种方法可以将这个表放在 1 个 foreach 中,这样我也可以使用我定义的模型使用 CRUD-Operations 编辑标题?
我已经尝试了很多来达到这个目标。无论如何,这是我在 Stackoverflow 上的第一个问题..
@model IEnumerable<Project_Lameau.Models.Dish>
@{
ViewBag.Title = "Index";
}
<h2>Suggestie</h2>
<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Suggestie")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />
<h2>Salade</h2>
<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Salade")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />
<h2>Voorgerecht</h2>
<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Voorgerecht")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />
<h2>Hoofd Vlees</h2>
<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Hoofd Vlees")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />
<h2>Hoofd Vis</h2>
<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Hoofd Vis")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
<hr />
<h2>Vega</h2>
<table class="table">
@foreach (var item in Model.Where(d => d.CourseName.Contains("Vega")))
{
<tr>
<td>@item.DishName</td>
<td class="text-right">@item.Price</td>
</tr>
}
</table>
【问题讨论】:
-
Dish类是什么样的? -
public class Dish { public int Id { get;放; } 公共 int CourseId { 获取;放; } 公共字符串 DishName { 获取;放; } 公共小数?价格{得到;放; } 公共课程课程 { 获取;放; } }