我正在尝试根据参数获取产品列表。从产品描述列表(Index.chtml)中,当我单击产品描述时,我想转到仅列出具有该产品描述的产品的页面。
例如:如果我点击Index.chtml页面上的“Barstools”,我想只浏览Browse.chtml页面上的barstools。
但我继续犯这个错误:
Exception Details: System.InvalidOperationException: Sequence contains no elements
Source Error:
Line 25: {
Line 26: //
Line 27: var productModel = productDB.ProductDess.Include("Products")
Line 28: .Single(p => p.productDesName == productD);
Line 29: return View(productModel);
这就是我所拥有的:
模型::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::
Product.cs
[Table("Product")]
[Bind(Exclude = "productID")]
public class Product
{
[ScaffoldColumn(false)]
[Key]
public string productID { get; set; }
public string productName { get; set; }
public string productDim { get; set; }
public string productWoodSp{ get; set; }
public string productFabric { get; set; }
public string productTop { get; set; }
public string productFinish { get; set; }
public string productAddInfo { get; set; }
public string productImgURL { get; set; }
public string productType { get; set; }
public string collectionID { get; set; }
public virtual Collection collection { get; set; }
public string productDesID { get; set; }
public virtual ProductDes ProductDes { get; set; }
}
ProductDes.cs
[Table("ProductDes")]
public class ProductDes
{
[Key]
public string productDesID { get; set; }
public string productDesName { get; set; }
public List<Product> Products { get; set; }
}
ProductEntities.cs
public class ProductEntities : DbContext
{
public DbSet<Collection> Collections { get; set; }
public DbSet<Project> Projects { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<ProductDes> ProductDess { get; set; }
}
控制器::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::
using System.Web.Mvc;
using StoreWeb.Models;
using System.Data.Entity;
namespace StoreWeb.Controllers
{
public class SampleController : Controller
{
ProductEntities productDB = new ProductEntities();
public ActionResult Index()
{
var productDes = productDB.ProductDess.ToList();
return View(productDes);
}
//
public ActionResult Browse(string productD)
{
//
var productModel = productDB.ProductDess.Include("Products")
.Single(p => p.productDesName == productD);
return View(productModel);
}
}
}
视图::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::::::::
@model StoreWeb.Models.ProductDes
@{
ViewBag.Title = "Browse" + Model.productDesName;
}
<h2>Browse</h2>
@foreach (var product in Model.Products)
{
<p />
<a href="@Url.Action("Details", new { id = product.productID })">
<img src="@product.productImgURL" alt="@product.productName" />
@product.productID @product.productName
</a>
}
有人可以请教我怎么做?谢谢!
答案 0 :(得分:4)
在控制器中,更改
var productModel = productDB.ProductDess.Include("Products")
.Single(p => p.productDesName == productD);
到
var productModel = productDB.ProductDess.Include("Products")
.SingleOrDefault(p => p.productDesName == productD);
在视图中,更改
@foreach (var product in Model.Products) {
}
到
@if (Model != null) {
foreach (var product in Model.Products) {
}
}