Ordering Entity Framework sub-items for EditorFor
I've seen Ordering sub-items within ordered items in a Linq to Entities Query which suggests that there is no way of getting the repository to r开发者_如何转开发eturn 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 ?
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.
精彩评论