Ordering Entity Framework sub-items for EditorFor

半城伤御伤魂 提交于 2019-12-01 06:47:40

问题


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 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:


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.



来源:https://stackoverflow.com/questions/3591726/ordering-entity-framework-sub-items-for-editorfor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!