查询附加的table based on not null in table values and return to view
Query additional table based on not null in table values and return to view
我正在尝试编写存储食谱的应用程序。然而,有些成分也可以是 "recipes"。我现在遇到的问题是如何为具有多种可能不同的较小食谱的东西提供完整的成分列表。抱歉,这有点长。这是模型:
public class Recipe
{
public int RecipeID { get; set; }
public string Recipe_Name { get; set; }
public int Labor_Cost { get; set; }
public virtual ICollection<RecipeDetail> RecipeDetails { get; set; }
}
public class RecipeDetail
{
public int RecipeDetailID {get; set;}
public int RecipeID { get; set; }
public int IngrediantID { get; set; }
public int Quantity { get; set; }
public virtual Recipe Recipe { get; set; }
public virtual Ingrediant Ingrediant { get; set; }
}
public class Ingrediant
{
public int IngrediantID { get; set; }
public string Ingrediant_Name { get; set; }
public bool? BreaksDown { get; set; }
public int? RecipeID { get; set; }
public virtual AHItem AHItems { get; set; }
}
所以这就是我的想法,我认为它会起作用,但我觉得它是非常正确的。我的第一个想法是将整个链接的数据库传递给视图,然后从那里提取我需要的东西。
所以控制器看起来像这样:(只是将所有内容发送到视图)。
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ViewBag.SelectedDetail = id;
Recipe recipe = db.Recipes;
if (recipe == null)
{
return HttpNotFound();
}
return View(recipe);
}
然后所有过滤都将在视图中进行...有点像这样(是的,这很丑陋,它会创建错误的 table 结构,一旦我弄明白了,我就会修复它).
@foreach (var item in Model.RecipeDetails)
{
if (item.RecipeID == ViewBag.SelectedDetail)
{
if (item.Ingrediant.AHItems != null)
{
x = x + item.Ingrediant.AHItems.Cost;
}
<tr>
<td>
@Html.DisplayFor(modelItem => item.Ingrediant.Ingrediant_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ingrediant.AHItems.Cost)
</td>
</tr>
if (item.Ingrediant.RecipeID != null)
{
y= item.Ingrediant.RecipeID;
foreach (var i in Model.RecipeDetails)
{
if (item.RecipeID == y)
{
<tr>
<th>Item Name</th>
<th>Amount Needed</th>
<th>AH Cost</th>
</tr>
<tr>
<td>
@Html.DisplayFor(modelItem => i.Ingrediant.Ingrediant_Name)
</td>
<td>
@Html.DisplayFor(modelItem => i.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => i.Ingrediant.AHItems.Cost)
</td>
</tr>
}
}
}
}
}
所以虽然我认为我可以完成上述工作,但在视图中似乎还有很多额外的工作要做。我该如何将其移至控制器中?
P.S。当 "recipe" 传递给视图时,有没有一种方法可以查看模型将生成的完整链接 table 看起来像什么,而无需编写代码来查看它?我想查看连接(以及查询的编写方式),以便更好地理解它在做什么。
将所有食谱传递给旨在仅显示一个的视图根本不是一个好主意。只获取您需要的食谱:
var recipe = db.Recipes.Find(id);
if (recipe == null)
{
return new HttpNotFoundResult();
}
但是,由于您有相关的实体也将在视图中使用,因此您应该将其稍微修改为:
var recipe = db.Recipes.Include("RecipeDetail.Recipe").Include("RecipeDetail.Ingredient").SingleOrDefault(m => m.Id == id);
这将为 RecipeDetail
发出联接,并且还会为 RecipeDetail
上的 Recipe
and/or Ingredient
属性发出联接。结果将是视图根据 id 需要的单个 Recipe
实例,它包含所有相关的 RecipeDetail
实例以及来自 Recipe
或 Ingredient
相关的信息每个 RecipeDetail
。您的视图现在在单个查询中拥有所需的所有信息,您不必将数据库转储到视图即可。
我正在尝试编写存储食谱的应用程序。然而,有些成分也可以是 "recipes"。我现在遇到的问题是如何为具有多种可能不同的较小食谱的东西提供完整的成分列表。抱歉,这有点长。这是模型:
public class Recipe
{
public int RecipeID { get; set; }
public string Recipe_Name { get; set; }
public int Labor_Cost { get; set; }
public virtual ICollection<RecipeDetail> RecipeDetails { get; set; }
}
public class RecipeDetail
{
public int RecipeDetailID {get; set;}
public int RecipeID { get; set; }
public int IngrediantID { get; set; }
public int Quantity { get; set; }
public virtual Recipe Recipe { get; set; }
public virtual Ingrediant Ingrediant { get; set; }
}
public class Ingrediant
{
public int IngrediantID { get; set; }
public string Ingrediant_Name { get; set; }
public bool? BreaksDown { get; set; }
public int? RecipeID { get; set; }
public virtual AHItem AHItems { get; set; }
}
所以这就是我的想法,我认为它会起作用,但我觉得它是非常正确的。我的第一个想法是将整个链接的数据库传递给视图,然后从那里提取我需要的东西。
所以控制器看起来像这样:(只是将所有内容发送到视图)。
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ViewBag.SelectedDetail = id;
Recipe recipe = db.Recipes;
if (recipe == null)
{
return HttpNotFound();
}
return View(recipe);
}
然后所有过滤都将在视图中进行...有点像这样(是的,这很丑陋,它会创建错误的 table 结构,一旦我弄明白了,我就会修复它).
@foreach (var item in Model.RecipeDetails)
{
if (item.RecipeID == ViewBag.SelectedDetail)
{
if (item.Ingrediant.AHItems != null)
{
x = x + item.Ingrediant.AHItems.Cost;
}
<tr>
<td>
@Html.DisplayFor(modelItem => item.Ingrediant.Ingrediant_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Ingrediant.AHItems.Cost)
</td>
</tr>
if (item.Ingrediant.RecipeID != null)
{
y= item.Ingrediant.RecipeID;
foreach (var i in Model.RecipeDetails)
{
if (item.RecipeID == y)
{
<tr>
<th>Item Name</th>
<th>Amount Needed</th>
<th>AH Cost</th>
</tr>
<tr>
<td>
@Html.DisplayFor(modelItem => i.Ingrediant.Ingrediant_Name)
</td>
<td>
@Html.DisplayFor(modelItem => i.Quantity)
</td>
<td>
@Html.DisplayFor(modelItem => i.Ingrediant.AHItems.Cost)
</td>
</tr>
}
}
}
}
}
所以虽然我认为我可以完成上述工作,但在视图中似乎还有很多额外的工作要做。我该如何将其移至控制器中?
P.S。当 "recipe" 传递给视图时,有没有一种方法可以查看模型将生成的完整链接 table 看起来像什么,而无需编写代码来查看它?我想查看连接(以及查询的编写方式),以便更好地理解它在做什么。
将所有食谱传递给旨在仅显示一个的视图根本不是一个好主意。只获取您需要的食谱:
var recipe = db.Recipes.Find(id);
if (recipe == null)
{
return new HttpNotFoundResult();
}
但是,由于您有相关的实体也将在视图中使用,因此您应该将其稍微修改为:
var recipe = db.Recipes.Include("RecipeDetail.Recipe").Include("RecipeDetail.Ingredient").SingleOrDefault(m => m.Id == id);
这将为 RecipeDetail
发出联接,并且还会为 RecipeDetail
上的 Recipe
and/or Ingredient
属性发出联接。结果将是视图根据 id 需要的单个 Recipe
实例,它包含所有相关的 RecipeDetail
实例以及来自 Recipe
或 Ingredient
相关的信息每个 RecipeDetail
。您的视图现在在单个查询中拥有所需的所有信息,您不必将数据库转储到视图即可。