asp.net mvc - System.InvalidOperationException: Sequence contains no elements [MVC] -


i'm trying list of products based on parameters. list of product descriptions (index.chtml), when click on product description, want go page lists products has product description.

for ex: if click on "barstools" on index.chtml page, want browse barstools on browse.chtml page.

but keep on getting error:

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); 

here's have:

models:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


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; } } 

controllers::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

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);     } } } 

views:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@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>       } 

can please advice me do? thank you!

in controller, change

var productmodel = productdb.productdess.include("products")     .single(p => p.productdesname == productd); 

to

var productmodel = productdb.productdess.include("products")     .singleordefault(p => p.productdesname == productd); 

in view, change

@foreach (var product in model.products) { } 

to

@if (model != null) {     foreach (var product in model.products) {     } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -