I wanted to create my own basic online shop so I had a look around and in the end chose to modify the MVC Music Store tutorial in to something tailored to my needs.
I am still in the early stages and so far, my version is very similar to the tutorial version with the exception of the tables and associated classes. Where the tutorial has Genres, Albums and Artists – I have a more simple Product and Category and I have adapted my project accordingly but I am getting an error.
It only happens in the StoreController\Browse section. When I try and view all items in a specific category, I get a System.InvalidOperationException “Sequence contains no elements”
The code used in the controller is:
public ActionResult Browse(string cat)
{
var categoryModel = storeDB.Categories.Include("Products")
.Single(g => g.CatName == cat);
return View(categoryModel);
}
And the view looks like this:
@model TPATK_shop.Models.Category
@{
ViewBag.Title = "Browse";
}
<h2>Browsing Category: @Model.CatName</h2>
<ul>
@foreach (var product in Model.Products)
{
<li>
@product.ProdName
</li>
}
</ul>
I have all of my classes and entity set up as follows:
namespace TPATK_shop.Models
{
public partial class Category
{
public int CategoryId { get; set; }
public string CatName { get; set; }
public string Description { get; set; }
public List<Product> Products { get; set; }
}
}
namespace TPATK_shop.Models
{
public class Product
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public string ProdName { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string ImageURL { get; set; }
public Category Category { get; set; }
}
}
namespace TPATK_shop.Models
{
public class TPATKStoreEntities : DbContext
{
public DbSet<Product> Products { get; set; }
public DbSet<Category> Categories { get; set; }
}
}
I have been researching the issue and I cant seem to find a solution. I have tried singleordefault but I still get the error.
I have confirmed that all the data is present in the DB and the keys are correct.
When my project fires up, an entry in the global.asax fires the following code to populate the DB (for test purposes – as detailed in the walkthrough)
namespace TPATK_shop.Models
{
public class SampleData : DropCreateDatabaseAlways<TPATKStoreEntities>
//public class SampleData : DropCreateDatabaseIfModelChanges<TPATKStoreEntities>
{
protected override void Seed(TPATKStoreEntities context)
{
var categories = new List<Category>
{
new Category { CatName = "50's" , Description = "50's stuff" },
new Category { CatName = "Weddings" , Description = "Wedding stuff" },
new Category { CatName = "Dribble Bibs" , Description = "Dribble bibs for kids" },
new Category { CatName = "Xmas" , Description = "Xmas stuff" },
};
new List<Product>
{
new Product { ProdName = "Headband 1", Description = "Des for headband 1", Category = categories.Single(g => g.CatName == "50's"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
new Product { ProdName = "Headband 2", Description = "Des for headband 2", Category = categories.Single(g => g.CatName == "Weddings"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
new Product { ProdName = "Headband 3", Description = "Des for headband 3", Category = categories.Single(g => g.CatName == "Dribble Bibs"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
new Product { ProdName = "Headband 4", Description = "Des for headband 4", Category = categories.Single(g => g.CatName == "Xmas"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
new Product { ProdName = "Headband 4", Description = "Des for headband 5", Category = categories.Single(g => g.CatName == "Xmas"), Price = 8.99M, ImageURL = "/Content/Images/placeholder.gif" },
}.ForEach(a => context.Products.Add(a));
}
}
}
Can anyone see why I am getting the error? Any help would be greatly received.
That is because the supplied
cathas not matching record in the database. UseSingleOrDefaultmethod to retrieve the entity, it will returnnullif there wasn’t a record.It maybe the case that MVC can not bind a value to the
catparameter and is set to null. So check whether you are passing a query string argument namedcat.