views:

41

answers:

1

I've seen http://stackoverflow.com/questions/3565249/ordering-sub-items-within-ordered-items-in-a-linq-to-entities-query which suggests that there is no way of getting the repository to return sub-items in an entity graph in a specific order.

If that's right, any thoughts on how to order the items in an EditorFor ?

i.e.

 //This works but returns a random order
 <%: Html.EditorFor(model => model.HPERDET.HORDERS) %>


 //This errors with "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."
 <%: Html.EditorFor(model => model.HPERDET.HORDERS.OrderBy(m=>m.APP_DATE)) %>

 //presorting the HORDERS into 
 //a public IOrderedEnumerable<HORDER> SortedHorders { get; set; } 
 //and ordering in my view model works, but breaks the binding because 
 //the generated html inputs no longer have the correct hierarchical names
 <%: Html.EditorFor(model => model.SortedHorders) %>

So is there a way to sort the sub-entities in graph in order to use them with EditorFor without resorting to assembling POCO objects duplicating the EF ones in all but order ?

+1  A: 

This is an excellent case for a ViewModel. ViewModels wrap the Entity Framework model and present the data in precisely the way required by the View for which it is designed. Perform the sorting in the ViewModel and bind the EditFor to the custom-sorted property.

Dave Swersky
I'm already using a ViewModel to pass additional data, but as the EF returns are quite large, I was trying to avoid traversing and copying into POCO objects. When you say "wrap the EF model" do you mean Wrap or "replicate the content but tuned to display": in this case all I need to change is the order so copying into my own classes is expensive for the benefits.
Andiih
@Aniih, I think your trying to micro-optimize. Generally anything you expect your users be comfortable using on the screen isn't large enough to worry about copying.
jfar
You are probably right, but the situation is I am displaying a large graph (tree) of info collapsed down then allowing the user to pick the info they see with jQuery. Perhaps I should do that with AJAX instead but sending everything in one slug (its an intranet so transmission bandwidth is not an issue) seemed like a good idea. Ill try the copy and see how I get on.
Andiih