Search code examples
c#asp.net-mvcrelated-content

Related data not showing asp.net c#


After reading this tutorial http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application I have created some models, controllers and views.

The recipes are showing just fine in the view, but I can't get the RecipeLines to show.

RecipeModel

public class RecipeModel
{
    [Key]
    public int RecipeId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public  string ImageUrl { get; set; }
    public virtual ICollection<RecipeLine> RecipeLines { get; set; }
}

RecipeLine

public class RecipeLine
{
    [Key]
    public int RecipeLineId { get; set; }
    public int RecipeId { get; set; }
    public double Quantity { get; set; }
    public UnitOfMeasureModel UnitOfMeasure { get; set; }
    public IngredientModel Ingredient { get; set; }
}

RecipeViewModel

public class RecipeViewModel
{
    public IEnumerable<RecipeModel> RecipeModels { get; set; }
    public IEnumerable<RecipeLine> RecipeLines { get; set; }
}

Recipecontroller

 public class RecipeController : Controller
    {
        private RecipeApplicationDb db = new RecipeApplicationDb();

        [HttpGet]
        public ActionResult Index(int? id)
        {
            var viewModel = new RecipeViewModel();
            viewModel.RecipeModels = db.Recipes
                //.Include(i => i.Name)
                .Include(i => i.RecipeLines);

            if (id != null)
                {
                ViewBag.RecipeId = id.Value;
                viewModel.RecipeLines = viewModel.RecipeModels.Where(i => i.RecipeId == id.Value).Single().RecipeLines;
                }

            return View(viewModel);
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RecipeModel recipeModel = db.Recipes.Find(id);
            if (recipeModel == null)
            {
                return HttpNotFound();
            }
            return View(recipeModel);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }

And the view

@model RecipeApplication.Models.RecipeViewModel

@{
    ViewBag.Title = "Recepten";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            Naam
        </th>
        <th>
            Omschrijving
        </th>
        <th>
            Afbeelding
        </th>
    </tr>

@foreach (var item in Model.RecipeModels) {
    string selectedRow = "";
    if(item.RecipeId == ViewBag.RecipeId)
    {
        selectedRow = "success";
    }
    <tr class="@selectedRow" valign="top">
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Description)
        </td>
        <td>
            @if (item.ImageUrl != null)
            {
                @Html.DisplayFor(modelItem => item.ImageUrl)
            }
        </td>
        <td>
            @Html.ActionLink("Select", "Index", new { id = item.RecipeId}) |
            @Html.ActionLink("Edit", "Edit", new { id=item.RecipeId }) |
            @Html.ActionLink("Details", "Details", new { id=item.RecipeId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.RecipeId })
        </td>
    </tr>
}
</table>

@if (Model.RecipeLines != null)
{
    foreach (var item in Model.RecipeLines)
    {
        string selectedRow = "";
        if (item.RecipeId == ViewBag.id)
        {
            <p>@item.Quantity @item.UnitOfMeasure @item.Ingredient</p>
        }
    }
}

When selecting the recipe, the line does get a proper color, and I see an id-value in the URL-string.

If someone could help with this one, that would be awesome.


Solution

  • You're comparing item.RecipeId to ViewBag.id, which doesn't exist. The ViewBag member you set in the controller action was ViewBag.RecipeId.

    However, you don't need this conditional at all. All of the recipe lines are already for that recipe id, because you specifically set only those recipe items in Model.RecipeLines.