2013-07-21 50 views
1
foreach (var i in Model.Items) 
{ 
     string s = "abc" + i.Name; 
} 

上面的代码给我InvalidOperationException,消息序列不包含任何元素。序列不包含任何元素 - IList <T>有项目

Model.Items是IList类型,它包含两个元素,尽管它在foreach循环中给我例外。

我在i.Name上应用了watch,它显示了一个值,但是当foreach循环内部的行被执行时,它会给出异常。

问题是什么?

堆栈跟踪

at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) 
    at ASP._Page_Views_Country_Hotels_cshtml.Execute() in d:\app\myController\items.cshtml:line 15 
    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy() 
    at System.Web.Mvc.WebViewPage.ExecutePageHierarchy() 
    at System.Web.WebPages.StartPage.RunPage() 
    at System.Web.WebPages.StartPage.ExecutePageHierarchy() 
    at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) 
    at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) 
    at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) 
    at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 
    at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) 

Model类

public class ItemsViewModel 
{ 
    public Category Category { get; set; } 
    public IList<Item> Items { get; set; } 
} 

线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 as suggested 
    foreach (var i in items) 
    { 
     desc += i.Name.ToString() + " ,";//this is where I am getting exception 
     //some other stuff 
    } 
} 

Item类

public class Item 
{ 
    [HiddenInput(DisplayValue = false)] 
    public int Id { get; set; } 

    [MaxLength(300, ErrorMessage = "Max length can be 300 only.")] 
    [Required] 
    [Remote("CheckItemName", "Item", AdditionalFields = "Id")] 
    public string Name { get; set; } 

    [Required] 
    [MinLength(50, ErrorMessage = "Min 50 characters should be entered.")] 
    [DataType(DataType.MultilineText)] 
    [AllowHtml] 
    [UIHint("tinymce_jquery_full")] 
    public string Description { get; set; } 
} 

CheckItemName方法

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

“i.Name”的类型是什么?如果你做'string s =“abc”+ i.Name.ToString()'?请点击[“将异常详细信息复制到剪贴板”](http://blogs.msdn.com/blogfiles/saraford/WindowsLiveWriter/Didyouknowyoucancopytheexceptiondetailsw_F67C/image_2.png)按钮,并通过编辑原始帖子粘贴整个异常。 –

+0

是i.Name一个字符串? – Krishna

+0

i.Name是字符串类型。 – Brij

回答

-2

虽然与ASP或MVC + SilverLight的工作,我不推荐使用第一个()或FirstOrDefault()。

Search .First();或.FirstOrDefault()并添加 - > .DefaultIfEmpty();

+3

'First'和'FirstOrDefault'具有完美定义的语义,非常适合于“ASP”和“MVC + Silverlight”(不管是什么)。如果为空,添加默认值只是给出不同的解决方案,并不适合每种情况。 –

相关问题