1

我正在尝试根据参数获取产品列表。从产品描述列表 (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);

这是我所拥有的:

楷模::::::::::::::::::::::::::::::::::::::::::::::::: ::::::::::::::::::::::::::::::


产品.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; }
}

产品描述文件


[Table("ProductDes")]
public class ProductDes
{
    [Key]
    public string productDesID { get; set; }
    public string productDesName { get; set; }

    public List<Product> Products { get; set; }
}

产品实体.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> 

    }

有人可以建议我该怎么做吗?谢谢!

4

1 回答 1

5

在控制器中,更改

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) {
    }
}
于 2012-06-05T01:12:06.873 回答