c# - Sequence contains no elements - IList<T> having items -


foreach (var in model.items) {         string s = "abc" + i.name; } 

above code giving me invalidoperationexception, message sequence contains no elements.

model.items of type ilist , contains 2 elements, despite of giving me exception in foreach loop.

i applied watch on i.name, shows value, when line inside foreach loop gets executed gives exception.

what issue ?

stack trace

   @ system.linq.enumerable.first[tsource](ienumerable`1 source)    @ asp._page_views_country_hotels_cshtml.execute() in d:\app\mycontroller\items.cshtml:line 15    @ system.web.webpages.webpagebase.executepagehierarchy()    @ system.web.mvc.webviewpage.executepagehierarchy()    @ system.web.webpages.startpage.runpage()    @ system.web.webpages.startpage.executepagehierarchy()    @ system.web.webpages.webpagebase.executepagehierarchy(webpagecontext pagecontext, textwriter writer, webpagerenderingbase startpage)    @ system.web.mvc.razorview.renderview(viewcontext viewcontext, textwriter writer, object instance)    @ system.web.mvc.buildmanagercompiledview.render(viewcontext viewcontext, textwriter writer)    @ system.web.mvc.viewresultbase.executeresult(controllercontext context)    @ system.web.mvc.controlleractioninvoker.invokeactionresult(controllercontext controllercontext, actionresult actionresult)    @ system.web.mvc.controlleractioninvoker.<>c__displayclass1a.<invokeactionresultwithfilters>b__17()    @ system.web.mvc.controlleractioninvoker.invokeactionresultfilter(iresultfilter filter, resultexecutingcontext precontext, func`1 continuation) 

model class

public class itemsviewmodel  {     public category category { get; set; }     public ilist<item> items { get; set; } } 

lines 1 15

@model ipf.web.models.itemsviewmodel  @{     layout = "_sublayout.cshtml";     string desc = "items of " + model.category.name + ". ";     string key = "items of " + model.category.name; }     <h1>@string.format("{0} - items", model.category.name)</h1> @if (model.items.count > 0) {     ilist<models.item> items = model.items.tolist();//added suggested     foreach (var in items)     {         desc += i.name.tostring() + " ,";//this getting exception         //some other stuff     } } 

item class

public class item {      [hiddeninput(displayvalue = false)]      public int id { get; set; }       [maxlength(300, errormessage = "max length can 300 only.")]      [required]      [remote("checkitemname", "item", additionalfields = "id")]      public string name { get; set; }       [required]      [minlength(50, errormessage = "min 50 characters should entered.")]      [datatype(datatype.multilinetext)]      [allowhtml]      [uihint("tinymce_jquery_full")]      public string description { get; set; } } 

checkitemname method

public jsonresult checkitemname([bind(prefix = "item")]item oitem) {     return json(!repository.items.where(c => c.id != oitem.id).any(c => c.name == oitem.name), jsonrequestbehavior.allowget); } 

while working asp or mvc + silverlight, dont recommend use first() or firstordefault().

search .first(); or .firstordefault() , add -> .defaultifempty();


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -