This is a migrated thread and some comments may be shown as answers.

Sequence contains no elements

2 Answers 212 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Donna Stewart
Top achievements
Rank 1
Donna Stewart asked on 22 Jun 2010, 10:05 PM
I am using the menu component in a web application written in VB - using VS 2010.  I am getting an InvalidOperationException when the menu is being built that says Sequence contains no elements.  When I debug the menu statement, it goes through the first element in the list then throws the exception.  I can see that my Model has 7 elements, so I am not sure where I am going wrong.  I am fairly new to asp.net so any help is very much appreciated!  Here is my code: 

The View:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of gmcps.PublicMenu))" %> 
 
<%@ Import Namespace="gmcps" %> 
 
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">  
    <script type="text/JavaScript">  
        window.onload = function () {  
            var settings = {  
                tl: { radius: 10 },  
                tr: { radius: 10 },  
                bl: { radius: 10 },  
                br: { radius: 10 },  
                antiAlias: true  
            }  
 
            curvyCorners(settings, "#publicmainleft");  
            curvyCorners(settings, "#publicmainright");  
            curvyCorners(settings, "#publiccontent");  
        }  
    </script> 
 
    <%Html.BeginForm()%> 
    <div> 
        <%= Html.Telerik.Menu() _  
            .Name("HomeMenu") _  
            .BindTo(Model,  
                    Sub(mappings)  
                        mappings.For(Of PublicMenu)(  
                            Sub(binding)  
                                binding _  
                                    .ItemDataBound(  
                                        Sub(item, publicMenu)  
                                            item.Text = publicMenu.mainMenuItem  
                                        End Sub) _  
                                    .Children(  
                                        Function(publicMenu)  
                                            Return publicMenu.subMenuItems  
                                        End Function)  
                            End Sub)  
                    End Sub)  
        %>    
    </div> 
    <div id="publiccontent">  
        <div class="publiccontentinner">  
            <div id="publicmainleft">  
                <class="GMCHomeHeader"><%= Html.lang("HomePageHeader")%><br /></p>  
                <class="GMCHomeContent"><%= Html.lang("HomePageContent")%></p>  
            </div> 
            <div id="publicmainright">  
                <p> 
                    <%= Html.SubmitImage("paperless", "/Content/Images/paperless-button.png")%> 
                    <%= Html.SubmitImage("findus", "/Content/Images/find-us.png")%> 
                    <%= Html.SubmitImage("summit", "/Content/Images/Summit-Button.png")%> 
                    <%= Html.SubmitImage("industrynews", "/Content/Images/Industry-news-button.png")%> 
                    <%= Html.SubmitImage("certs", "/Content/Images/Certifications-button.png")%> 
                </p> 
            </div> 
        </div> 
    </div> 
    <%Html.EndForm()%> 
</asp:Content> 
 

PublicMenu class:

 

 

 

Public Class PublicMenu  
 
    Public Property mainMenuItem As String  
    Public Property subMenuItems As List(Of String)  
 
End Class  
 
Here is the stack trace:
[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.First(IEnumerable`1 source) +336
   Telerik.Web.Mvc.UI.NavigationItemContainerExtensions.Bind(TNavigationItem component, Object dataItem, NavigationBindingFactory`1 factory) +345
   Telerik.Web.Mvc.UI.NavigationItemContainerExtensions.Bind(TNavigationItem component, Object dataItem, NavigationBindingFactory`1 factory) +651
   Telerik.Web.Mvc.UI.NavigationItemContainerExtensions.BindTo(INavigationItemContainer`1 component, IEnumerable dataSource, Action`1 factoryAction) +384
   Telerik.Web.Mvc.UI.MenuBuilder.BindTo(IEnumerable dataSource, Action`1 factoryAction) +70
   ASP.views_home_index_aspx.__RenderindexContent(HtmlTextWriter __w, Control parameterContainer) in C:\CVS\GMCWeb\gmcps\Views\Home\Index.aspx:30
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in C:\CVS\GMCWeb\gmcps\Views\Shared\Site.Master:3
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +109
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Control.Render(HtmlTextWriter writer) +10
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +208
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +8
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +56
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +100
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3060

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 23 Jun 2010, 06:57 AM
Hi Donna Stewart,

We weren't able to reproduce the problem based on the provided description and code. I suggest you open a support ticket and send us a sample project showing the exception.

Kind regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Donna Stewart
Top achievements
Rank 1
answered on 23 Jun 2010, 06:55 PM
I fixed this - I was trying to use strings and list of strings for my menu and submenus.  Maybe this will help someone else who may be as lost as I was on how to use the Telerik MVC menu. 

Here is my view code:
<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/PublicSite.master" Inherits="System.Web.Mvc.ViewPage(Of IEnumerable (Of gmcps.PublicMenu))" %> 
 
<%@ Import Namespace="gmcps" %> 
 
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">  
    <script type="text/JavaScript">  
        window.onload = function () {  
            var settings = {  
                tl: { radius: 10 },  
                tr: { radius: 10 },  
                bl: { radius: 10 },  
                br: { radius: 10 },  
                antiAlias: true  
            }  
 
            curvyCorners(settings, "#publicmainleft");  
            curvyCorners(settings, "#publicmainright");  
            curvyCorners(settings, "#publiccontent");  
        }  
    </script> 
 
    <%Html.BeginForm()%> 
    <div> 
        <%= Html.Telerik.Menu() _  
            .Name("HomeMenu") _  
            .BindTo(Model,  
                    Sub(mappings)  
                        mappings.For(Of PublicMenu)(  
                            Sub(binding)  
                                binding _  
                                    .ItemDataBound(  
                                        Sub(item, publicMenu)  
                                            item.Text = publicMenu.mainMenuItem.Text  
                                            If Not String.IsNullOrWhiteSpace(publicMenu.mainMenuItem.ActionName) Then  
                                                item.ActionName = publicMenu.mainMenuItem.ActionName  
                                            End If  
                                            If Not String.IsNullOrWhiteSpace(publicMenu.mainMenuItem.ControllerName) Then  
                                                item.ControllerName = publicMenu.mainMenuItem.ControllerName  
                                            End If  
                                        End Sub) _  
                                    .Children(  
                                    Function(publicMenu)  
                                        Return publicMenu.subMenuItems  
                                    End Function)  
                            End Sub)  
                        mappings.For(Of Telerik.Web.Mvc.UI.MenuItem)(  
                                Sub(binding)  
                                    binding.ItemDataBound(  
                                        Sub(item, subMenu)  
                                            item.Text = subMenu.Text  
                                            If Not String.IsNullOrWhiteSpace(subMenu.ActionName) Then  
                                                item.ActionName = subMenu.ActionName  
                                            End If  
                                            If Not String.IsNullOrWhiteSpace(subMenu.ControllerName) Then  
                                                item.ControllerName = subMenu.ControllerName  
                                            End If  
                                        End Sub)  
                                End Sub)  
                    End Sub)  
        %>    
    </div> 
    <div id="publiccontent">  
        <div class="publiccontentinner">  
            <div id="publicmainleft">  
                <class="GMCHomeHeader"><%= Html.lang("HomePageHeader")%><br /></p>  
                <class="GMCHomeContent"><%= Html.lang("HomePageContent")%></p>  
            </div> 
            <div id="publicmainright">  
                <p> 
                    <%= Html.SubmitImage("paperless", "/Content/Images/paperless-button.png")%> 
                    <%= Html.SubmitImage("findus", "/Content/Images/find-us.png")%> 
                    <%= Html.SubmitImage("summit", "/Content/Images/Summit-Button.png")%> 
                    <%= Html.SubmitImage("industrynews", "/Content/Images/Industry-news-button.png")%> 
                    <%= Html.SubmitImage("certs", "/Content/Images/Certifications-button.png")%> 
                </p> 
            </div> 
        </div> 
    </div> 
    <%Html.EndForm()%> 
</asp:Content> 
 

Here is the controller:
Imports System.Resources  
Imports gmcps.Utilities  
 
Public Class HomeController  
    Inherits System.Web.Mvc.Controller  
 
    Function Index() As ActionResult  
        Dim menuList As List(Of PublicMenu) = New List(Of PublicMenu)  
        Dim tempList As List(Of ResxItem)  
        tempList = GetResXResources("App_GlobalResources\HomePage.resx")  
        tempList.Sort()  
        Dim prevItem As String = tempList.Item(0).Name  
        Dim menuListIndex As Integer = 0 
        Dim mmItem As New Telerik.Web.Mvc.UI.MenuItem()  
        mmItem.Text = tempList.Item(0).Value  
        mmItem.ActionName = "Home" 
        mmItem.ControllerName = "Home" 
        menuList.Add(New PublicMenu With {.mainMenuItem = mmItem.subMenuItems = New List(Of Telerik.Web.Mvc.UI.MenuItem)})  
        menuList.Item(0).subMenuItems.Add(mmItem)  
        For Each tempItem In tempList  
            mmItem = New Telerik.Web.Mvc.UI.MenuItem()  
            mmItem.ControllerName = "Home" 
            mmItem.ActionName = tempItem.Value  
            mmItem.Text = tempItem.Value  
            If tempList.IndexOf(tempItem) > 0 Then  
                If tempItem.Name.Substring(0, 10) = prevItem.Substring(0, 10) Then  
                    menuList.Item(menuListIndex).subMenuItems.Add(mmItem)  
                Else  
                    menuList.Add(New PublicMenu With {.mainMenuItem = mmItem.subMenuItems = New List(Of Telerik.Web.Mvc.UI.MenuItem)})  
                    menuListIndexmenuListIndex = menuListIndex + 1  
                    menuList.Item(menuListIndex).subMenuItems.Add(mmItem)  
                End If  
                prevItem = tempItem.Name  
            End If  
        Next  
        Return (View(menuList))  
    End Function  
 
    Function About() As ActionResult  
        Return View()  
    End Function  
End Class  
 
Here is the PublicMenu class:
 
Public Class PublicMenu  
 
    Public Property mainMenuItem As Telerik.Web.Mvc.UI.MenuItem  
    Public Property subMenuItems As List(Of Telerik.Web.Mvc.UI.MenuItem)  
 
End Class  
 

The controller is, of course, not complete, but it shows how I build out the Model for the menu.  This may not be the best way, but it works for what I need.  If anyone has suggestions or comments, feel free...

Oh, and the GetResXResources is something I "borrowed" from Rick Strahl to read a resx file as an xml doc so I could process the menu items.  You can see Rick's blog here - http://www.west-wind.com/weblog/ShowPost.aspx?id=698097.  Thank you, Rick!

Thanks!
Tags
Menu
Asked by
Donna Stewart
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Donna Stewart
Top achievements
Rank 1
Share this question
or