Just getting started with Visual Studio, .NET, C#, MVC3 etc so please go easy on me.
Background:
I am creating an MVC3 web application, SQL Server 2008, DB first with a generated model (emdx).
I have followed tutorials (most go through Code first / Model first) and not DB first.
I am doing it DB first as I am building an inventory control system with Bill of Materials functionality and Entity Framework seems not to support adjacency lists.
My problem however is much more simple than the recursion.
Problem:
Table1: Categories
CategoryId int, pk
Name nvarchar(50)
Navigation Properties : Parts
Table2: Parts
PartId int, pk
Name nvarchar(50)
CategoryId int FK
I am able to list ALL categories.
I am able to list ALL parts.
I am unable to list ALL parts which belong to a category.
I have tried to implement this
public ActionResult Browse(string genre)
{
// Retrieve Genre and its Associated Albums from database
var genreModel = storeDB.Genres.Include("Albums")
.Single(g => g.Name == genre);
return View(genreModel);
}
@model EM6.Models.Category
@{
ViewBag.Title = "Browse";
}
<h2>Browsing Category @Model.Name</h2>
<ul>
@foreach (var part in Model.Parts)
{
<li>@part.Name</li>
}
</ul>
but I get the following error message.
var categoryModel = _db.Categories.Include("Parts")
.Single(c => c.Name == myCategory);
InvalidOperationException was unhandled by user code
Sequence contains no elements
var categoryModel = _db.Categories.Include("Parts").Where(c => c.Name == myCategory).FirstOrDefault();
if( categoryModel == null)
return Content(" does not exists category with name : " + myCategory)
else
return View(categoryModel ); // or what you have
<h2>Browsing Category @Model.Name</h2>
@if(Model.Parts == null ||Model.Parts.Count() == 0){<text> no parts in model</text>}
<ul>
@foreach (var part in Model.Parts)
{
<li>@part.Name</li>
}
</ul>
MangoMM
Member
65 Points
55 Posts
Help with navigating using Navigation Properties???
Feb 22, 2012 09:11 PM|LINK
Hi there,
Just getting started with Visual Studio, .NET, C#, MVC3 etc so please go easy on me.
Background:
I am creating an MVC3 web application, SQL Server 2008, DB first with a generated model (emdx).
I have followed tutorials (most go through Code first / Model first) and not DB first.
I am doing it DB first as I am building an inventory control system with Bill of Materials functionality and Entity Framework seems not to support adjacency lists.
My problem however is much more simple than the recursion.
Problem:
Table1: Categories
CategoryId int, pk
Name nvarchar(50)
Navigation Properties : Parts
Table2: Parts
PartId int, pk
Name nvarchar(50)
CategoryId int FK
I am able to list ALL categories.
I am able to list ALL parts.
I am unable to list ALL parts which belong to a category.
I have tried to implement this
public ActionResult Browse(string genre) { // Retrieve Genre and its Associated Albums from database var genreModel = storeDB.Genres.Include("Albums") .Single(g => g.Name == genre); return View(genreModel); }from this tutorial http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4
View:
@model EM6.Models.Category @{ ViewBag.Title = "Browse"; } <h2>Browsing Category @Model.Name</h2> <ul> @foreach (var part in Model.Parts) { <li>@part.Name</li> } </ul>but I get the following error message.
var categoryModel = _db.Categories.Include("Parts") .Single(c => c.Name == myCategory); InvalidOperationException was unhandled by user code Sequence contains no elementsYes there is data in the database!
Any help would be much appreciated.
Thanks
ignatandrei
All-Star
134527 Points
21579 Posts
Moderator
MVP
Re: Help with navigating using Navigation Properties???
Feb 22, 2012 09:14 PM|LINK
var categoryModel = _db.Categories.Include("Parts").Where(c => c.Name == myCategory).FirstOrDefault(); if( categoryModel == null) return Content(" does not exists category with name : " + myCategory) else return View(categoryModel ); // or what you haveMangoMM
Member
65 Points
55 Posts
Re: Help with navigating using Navigation Properties???
Feb 22, 2012 09:26 PM|LINK
Thank you for the very quick reply.
Now I am not getting the exception, but the view is not showing any Parts, just an empty Unordered list.
Here is my Index view:
@model IEnumerable<EM6.Models.Category> @{ ViewBag.Title = "Categories"; } <h2>Browse Categories</h2> <p>Select from @Model.Count() categories</p> <ul> @foreach (var category in Model) { <li> @Html.ActionLink( category.Name, "Browse", new { myCategory = category.Name } ) </li> } </ul>Here is my Browse View:
@model EM6.Models.Category @{ ViewBag.Title = "Browse"; } <h2>Browsing Category @Model.Name</h2> <ul> @foreach (var part in Model.Parts) { <li>@part.Name</li> } </ul>Here is a snippet of the generated HTML:
<section id="main"> <h2>Browsing Category Service Parts</h2> <ul> </ul> </section> <footer> </footer>ignatandrei
All-Star
134527 Points
21579 Posts
Moderator
MVP
Re: Help with navigating using Navigation Properties???
Feb 23, 2012 06:58 AM|LINK
<h2>Browsing Category @Model.Name</h2> @if(Model.Parts == null ||Model.Parts.Count() == 0){ <text> no parts in model</text> } <ul> @foreach (var part in Model.Parts) { <li>@part.Name</li> } </ul>MangoMM
Member
65 Points
55 Posts
Re: Help with navigating using Navigation Properties???
Feb 23, 2012 08:57 AM|LINK
Perfect. Works now. Not sure why, but it does. It is pulling in Model.Parts as it should. Thanks for your help.