代码之家  ›  专栏  ›  技术社区  ›  Andiih

为editorfor排序实体框架子项

  •  3
  • Andiih  · 技术社区  · 15 年前

    我见过 Ordering sub-items within ordered items in a Linq to Entities Query 这意味着没有办法让存储库以特定的顺序返回实体图中的子项。

    如果是这样的话,有没有想过如何在一个编辑中为这些项目排序?

     //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) %>
    

    那么,是否有一种方法可以对图中的子实体进行排序,以便在不使用汇编POCO对象的情况下将它们与editorfor一起使用?除了顺序之外,所有的ef对象都是重复的?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Dave Swersky    15 年前

    这是ViewModel的一个很好的例子。视图模型包装实体框架模型,并以视图设计所需的方式精确地呈现数据。在视图模型中执行排序,并将editfor绑定到自定义排序属性。