我想創建自己的基本網上商店,所以我查看了一下,最後選擇修改MVC音樂商店教程以適應我的需求。MVC音樂商店變體 - 「序列不包含任何元素」錯誤
我還處於早期階段,到目前爲止,我的版本與教程版本非常相似,但表格和關聯類除外。該教程有流派,專輯和藝術家 - 我有一個更簡單的產品和類別,我已經適應了我的項目,但我得到一個錯誤。
它只發生在StoreController \ Browse部分。當我嘗試並查看特定類別的所有項目,我得到一個System.InvalidOperationException「序列不包含任何元素」
在控制器中使用的代碼是:
public ActionResult Browse(string cat)
{
var categoryModel = storeDB.Categories.Include("Products")
.Single(g => g.CatName == cat);
return View(categoryModel);
}
和視圖看起來是這樣的:
@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>
我有我所有的類和實體設置爲如下:
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; }
}
}
我一直在研究這個問題,我似乎找不到解決方案。我試過singleordefault,但我仍然得到錯誤。
我已經確認所有數據都存在於數據庫中,並且密鑰是正確的。
當我的項目激發起來,在Global.asax中的條目觸發下面的代碼來填充DB(用於測試目的 - 如在演練中詳述)
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));
}
}
}
有人能看到爲什麼我收到錯誤?任何幫助都會得到很大的迴應。
從視圖中刪除你的foreach代碼片刻,並檢查'@ Model.CatName'和'@ Model.Products.Count()'顯示 – 2012-01-03 11:22:42
Pankej - 我已經完成了你的建議,並且仍然出現錯誤。它甚至在它看到之前就打破了。 – 2012-01-03 11:36:54
你現在得到什麼錯誤... – 2012-01-03 11:42:53