Help with AutoMapper and Sorting please!

This may be a little more up the alley of our mvc fellows, but regardless, I need some help.

First, I have the following model:

using System.Collections.Generic;
namespace Venue.Models
public class ForumManagerModel
public int Id { get; set; }
public int Sequence { get; set; }
public string Title { get; set; }
public IEnumerable<ForumManagerModel> Children { get; set; }

I have the following in Global.asax:

Mapper.CreateMap<Forum, ForumManagerModel>();

And I have the following action:

public ActionResult Manager()
return View(Mapper.Map<IEnumerable<Forum>, IEnumerable<ForumManagerModel>>(forumRepository.SelectByParentId(null)));

I have the following in my Manager.aspx view:

<&#37; Html.RenderPartials("Manager_ChildBit", Model.OrderBy(x => x.Sequence)); %>

And the following in my Manager_ChildBit.ascx partial:

<% Html.RenderPartials("Manager_ChildBit", Model.Children.OrderBy(x => x.Sequence)); %>

Now the problem: it isn’t applying the orderby clauses!


Notes: RenderPartials is a custom extension method and that part works, as do the actual views. The correct data is displayed. Just not in the expected order. In fact, it mirrors the database.

Then Group and Order :slight_smile:

I got this one sorted (no pun intended). Thanks.

Why not do the sorting within your SelectByParentId method?

That won’t fully work, unless there’s some linq trick I am not aware of. Let’s say I do that, and then ask for SelectByParentId(8). All those forums who directly hang off of id 8 will be ordered by sequencce, but each of those forum has children too, and each of those may have children also, none of which will be sorted. Remember the parenting is Forum -> Forum -> Forum …

I suspect that the problem is somewhere in the RenderPartials extension method. Partials are using the model passed in the control (the original IEnumerable) and not the one being passed as a parameter.