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

[Solved] Sort order of children

7 Answers 76 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.
Chase
Top achievements
Rank 1
Chase asked on 26 Sep 2010, 02:29 AM
Hi!

I just started using the Telerik component UI for MVC, so far I really enjoy it, but I am having a hard time with one thing.

I would like to be able to sort the way my children are listed on the menu is this possible?

Thank you for any advise or pointers in the right direction.

Chase

7 Answers, 1 is accepted

Sort by
0
Hristo Germanov
Telerik team
answered on 27 Sep 2010, 01:48 PM
Hello Chase,

I have attached project, which shows our Telerik ASP.NET MVC menu with server binding. Check the Index Action method to see how collection bound to the menu is sorted.

Greetings,

Hristo Germanov

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
Chase
Top achievements
Rank 1
answered on 27 Sep 2010, 05:46 PM
Well, that got me in the right direction using the DataBindingToModel controller in the example projects Telerik.Web.Mvc.Examples

namespace Telerik.Web.Mvc.Examples
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Timers;
    using System.Web.Mvc;
    using Telerik.Web.Mvc.Examples.Models;
 
    public partial class MenuController : Controller
    {
        public ActionResult DataBindingToModel()
        {          
             
            NorthwindDataContext northwind = new NorthwindDataContext();
 
            var sortedList = getData().OrderBy(e => e.LastName);
            foreach (var item in sortedList)
                item.Orders.OrderBy(o => o.Customer.CompanyName);
                ;
 
            return View(sortedList);
        }
        private IEnumerable<Employee> getData()
        {
            return getData();
        }
    }
}


and the corresponding view

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Employee>>" %>
 
 
<asp:content contentplaceholderid="MainContent" runat="server">
 
<%= Html.Telerik().Menu()
        .Name("Menu")
        .BindTo(Model, mappings =>
        {
            mappings.For<Employee>(binding => binding
                    .ItemDataBound((item, employee) =>
                    {
                        item.Text = employee.FirstName;
                    })
                    .Children(employee => employee.Orders));
            mappings.For<Order>(binding => binding
                    .ItemDataBound((item, order) =>
                    {
                        item.Text = order.Customer.CompanyName + " " + order.OrderDate;
                    }));
        })
%>
 
</asp:content>

Everything complied and looks like this should run correctly but when I try to go to this page the server crashes

WebDev.WebServer20.exe has stopped working

Description:
  Stopped working

Problem signature:
  Problem Event Name:   CLR20r3
  Problem Signature 01:   webdev.webserver20.exe
  Problem Signature 02:   10.0.0.0
  Problem Signature 03:   4ba204ca
  Problem Signature 04:   Telerik.Web.Mvc.Examples
  Problem Signature 05:   1.0.0.0
  Problem Signature 06:   4ca0c8cc
  Problem Signature 07:   179
  Problem Signature 08:   1
  Problem Signature 09:   System.StackOverflowException
  OS Version:   6.1.7600.2.0.0.256.1
  Locale ID:   1033

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409

If the online privacy statement is not available, please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt

0
Chase
Top achievements
Rank 1
answered on 27 Sep 2010, 07:27 PM
Ok so I got it to stop crashing and the parent gets sorted buyt the children are still not sorting.

namespace Telerik.Web.Mvc.Examples
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Timers;   
    using Telerik.Web.Mvc.Examples.Models;
 
    public partial class MenuController : Controller
    {
        private static NorthwindDataContext northwind = new NorthwindDataContext();
 
        public ActionResult DataBindingToModel()
        {          
             
             
 
            var sortedList = getData().OrderBy(e => e.FirstName);
            foreach (var item in sortedList)
               item.Orders.OrderBy(o => o.Customer.CompanyName);
               ;
 
            return View(sortedList);
        }
        private IEnumerable<Employee> getData()
        {
            return northwind.Employees;
        }
    }
}
0
Atanas Korchev
Telerik team
answered on 28 Sep 2010, 08:35 AM
Hi Chase,

Does the attached sample project work for you? How is your code different from the sample?

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
Chase
Top achievements
Rank 1
answered on 28 Sep 2010, 10:01 PM
The project you provided work but not when you try to use it on a linq entity.

using the telerik examples that come with the library. if I use northwind.Employees as the parent and try to resolve the orders of each employee sorted by name and then the orders sorted by name of the company
foreach (var item in sortedList)
                item.Orders = item.Orders.OrderBy(o => o.Customer.CompanyName);


I get this error

Error   1   Cannot implicitly convert type 'System.Linq.IOrderedEnumerable<Telerik.Web.Mvc.Examples.Models.Order>' to 'System.Data.Linq.EntitySet<Telerik.Web.Mvc.Examples.Models.Order>'   C:\Program Files\Telerik\Extensions for ASP.NET MVC Q2 2010\Examples\Telerik.Web.Mvc.Examples\Controllers\Menu\DataBindingToModelController.cs   20   31   Telerik.Web.Mvc.Examples


this only happens when I try to use the OrderBy method

if I just have

foreach (var item in sortedList)
                item.Orders = item.Orders;

then it works fine but is about as productive as doing nothing at all.

so that is where I am at with it

if you have any thought it would be greatly appreciated.

Thank you!!

Chase
0
Hristo Germanov
Telerik team
answered on 29 Sep 2010, 09:23 AM
Hi Chase,

If you want to convert IEnumerable to EntitySet you need to write your own method which will do that for you. Your method needs to look like this:

public static EntitySet<T> ToEntitySet<T> (this IEnumerable<T> source) where T : class
{
    var es = new EntitySet<T> ();
    es.AddRange (source);
    return es;
}

The second solution is to add custom class:

public class CustomEmployee
{
    public string FirstName { get; set; }
    public List<Order> Orders { get; set; }
}

Contoller methods:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var sortedList = from e in GetData()
                         orderby e.FirstName
                         select new CustomEmployee
                         {
                             FirstName = e.FirstName,
                             Orders = e.Orders.OrderByDescending(o => o.Customer.CompanyName).ToList()
                         };
 
        return View(sortedList);
    }
 
    private IQueryable<Employee> GetData()
    {
        return new NorthwindDataContext().Employees;
    }
}

All the best,
Hristo Germanov
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
Chase
Top achievements
Rank 1
answered on 29 Sep 2010, 04:40 PM
That works perfectly!!

Thank you very much for you time Hristo, it is greatly appreciated.
Tags
Menu
Asked by
Chase
Top achievements
Rank 1
Answers by
Hristo Germanov
Telerik team
Chase
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or