1. Category (categories of recipes...Desert, Protein, Carbohydrate, etc.)
2. Recipes
3. Ingredients (for each recipe)
Error below occurs when navigating to 'Details action method / view.
Browse action method successfully displays recipes belonging to a given category.
I am unsuccessfully trying to mirror the logic from Browse to Details action methods and views in order to display the ingredients that belong to a given recipe (I've been working at this for several hours yesterday and again today, altering variables in the
code, but I'm just not getting it).
Code and error follows...
__________________________
Server Error in '/' Application
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[MvcRecipe.Models.Recipe]', but this dictionary requires a model item of type 'MvcRecipe.Models.Recipe'.
__________________________
MvcRecipe\Controllers\RecipeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcRecipe.Models;
namespace MvcRecipe.Models
{
public class RecipeController : Controller
{
RecipeEntities recipeDB = new RecipeEntities();
//
// GET: /Recipe/
public ActionResult Index()
{
var category = recipeDB.Categories.ToList();
return View(category);
}
//
// GET: /Recipe/Browse
public ActionResult Browse(string category)
{
// Retrieve Category and its Associated Recipes from database
var categoryModel = recipeDB.Categories.Include("Recipes")
.Single(c => c.Name == category);
return View(categoryModel);
}
//
// GET: /Recipe/Details
public ActionResult Details(int id)
{
// Retrieve Recipe and its Associated Ingredients from database
var ingredientModel = recipeDB.Recipes.Include("Ingredients");
<ul>
@foreach (var recipe in Model.Recipes)
{
<li>
@Html.ActionLink(recipe.Name,
"Details", new { id = recipe.RecipeId })
</li>
}
</ul>
_________________________
MvcRecipe\Views\Recipe\Details.cshtml
@model MvcRecipe.Models.Recipe
@{
ViewBag.Title = "Details";
}
<h2>Recipe: @Model.Name </h2>
<ul>
@foreach (var ingredient in Model.Ingredients)
{
<li>
@Html.ActionLink(ingredient.Name,
"Browse", new { ingredient = ingredient.Name })
</li>
}
</ul>
_________________________
MvcRecipe\Models\Recipe.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcRecipe.Models
{
public class Recipe
{
public int RecipeId { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public string Name { get; set; }
public string Version1 { get; set; }
public string Version2 { get; set; }
public int IngredientId { get; set; }
public Ingredient Ingredient { get; set; }
public List<Ingredient> Ingredients { get; set; }
}
}
_________________________
MvcRecipe\Models\Category.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcRecipe.Models
{
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public List<Recipe> Recipes { get; set; }
}
}
_________________________
MvcRecipe\Models\Ingredient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcRecipe.Models
{
public class Ingredient
{
public int IngredientId { get; set; }
public int RecipeId { get; set; }
public virtual Recipe Recipe { get; set; }
public string Name { get; set; }
public string Variation { get; set; }
public IEnumerator<Recipe> Recipes { get; set; }
}
}
________________________________________
public ActionResult Details(int id)
{
// Retrieve Recipe and its Associated Ingredients from database
var ingredientModel = recipeDB.Recipes.Include("Ingredients").Where(item=>item.Id == id).FirstOrDefault();
if(ingredientModel == null)
return Content(" no such ingredinet with id = "+ id);
3v3rhart
Member
159 Points
317 Posts
Displaying son / daughter data under father model
Dec 21, 2012 04:28 PM|LINK
Setup:
Recipe Project with three tables':
1. Category (categories of recipes...Desert, Protein, Carbohydrate, etc.)
2. Recipes
3. Ingredients (for each recipe)
Error below occurs when navigating to 'Details action method / view.
Browse action method successfully displays recipes belonging to a given category.
I am unsuccessfully trying to mirror the logic from Browse to Details action methods and views in order to display the ingredients that belong to a given recipe (I've been working at this for several hours yesterday and again today, altering variables in the code, but I'm just not getting it).
Code and error follows...
__________________________
Server Error in '/' Application
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[MvcRecipe.Models.Recipe]', but this dictionary requires a model item of type 'MvcRecipe.Models.Recipe'.
__________________________
MvcRecipe\Controllers\RecipeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcRecipe.Models;
namespace MvcRecipe.Models
{
public class RecipeController : Controller
{
RecipeEntities recipeDB = new RecipeEntities();
//
// GET: /Recipe/
public ActionResult Index()
{
var category = recipeDB.Categories.ToList();
return View(category);
}
//
// GET: /Recipe/Browse
public ActionResult Browse(string category)
{
// Retrieve Category and its Associated Recipes from database
var categoryModel = recipeDB.Categories.Include("Recipes")
.Single(c => c.Name == category);
return View(categoryModel);
}
//
// GET: /Recipe/Details
public ActionResult Details(int id)
{
// Retrieve Recipe and its Associated Ingredients from database
var ingredientModel = recipeDB.Recipes.Include("Ingredients");
return View(ingredientModel);
}
}
}
__________________________
MvcRecipe\Views\Recipe\Browse.cshtml
@model MvcRecipe.Models.Category
@{
ViewBag.Title = "Browse";
}
<h2>Browsing Category: @Model.Name</h2>
<ul>
@foreach (var recipe in Model.Recipes)
{
<li>
@Html.ActionLink(recipe.Name,
"Details", new { id = recipe.RecipeId })
</li>
}
</ul>
_________________________
MvcRecipe\Views\Recipe\Details.cshtml
@model MvcRecipe.Models.Recipe
@{
ViewBag.Title = "Details";
}
<h2>Recipe: @Model.Name </h2>
<ul>
@foreach (var ingredient in Model.Ingredients)
{
<li>
@Html.ActionLink(ingredient.Name,
"Browse", new { ingredient = ingredient.Name })
</li>
}
</ul>
_________________________
MvcRecipe\Models\Recipe.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcRecipe.Models
{
public class Recipe
{
public int RecipeId { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public string Name { get; set; }
public string Version1 { get; set; }
public string Version2 { get; set; }
public int IngredientId { get; set; }
public Ingredient Ingredient { get; set; }
public List<Ingredient> Ingredients { get; set; }
}
}
_________________________
MvcRecipe\Models\Category.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcRecipe.Models
{
public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
public List<Recipe> Recipes { get; set; }
}
}
_________________________
MvcRecipe\Models\Ingredient.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcRecipe.Models
{
public class Ingredient
{
public int IngredientId { get; set; }
public int RecipeId { get; set; }
public virtual Recipe Recipe { get; set; }
public string Name { get; set; }
public string Variation { get; set; }
public IEnumerator<Recipe> Recipes { get; set; }
}
}
________________________________________
ignatandrei
All-Star
134527 Points
21579 Posts
Moderator
MVP
Re: Displaying son / daughter data under father model
Dec 21, 2012 07:40 PM|LINK
public ActionResult Details(int id)
{
// Retrieve Recipe and its Associated Ingredients from database
var ingredientModel = recipeDB.Recipes.Include("Ingredients").Where(item=>item.Id == id).FirstOrDefault();
if(ingredientModel == null)
return Content(" no such ingredinet with id = "+ id);
else
return View(ingredientModel);