c# - 为 EditorFor 订购 Entity Framework 子项

标签 c# asp.net-mvc entity-framework asp.net-mvc-2 entity-framework-4

我看过 Ordering sub-items within ordered items in a Linq to Entities Query这表明无法让存储库以特定顺序返回实体图中的子项。

如果那是对的,关于如何在 EditorFor 中排序项目有什么想法吗?

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

那么有没有一种方法可以对图中的子实体进行排序,以便将它们与 EditorFor 一起使用,而无需诉诸组装 POCO 对象,除了按顺序复制 EF 对象外?

最佳答案

这是 ViewModel 的绝佳案例。 ViewModels 包装了 Entity Framework 模型并以其设计的 View 所需的方式精确呈现数据。在 ViewModel 中执行排序并将 EditFor 绑定(bind)到自定义排序的属性。

关于c# - 为 EditorFor 订购 Entity Framework 子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3591726/

相关文章:

c# - 如何将通用委托(delegate)参数传递给 .NET 2.0 中的方法

asp.net-mvc - 是否有任何带有 TDD 和 MOQ 源代码的好的 ASP.Net MVC 项目可用?

asp.net-mvc - 如何让我的 MVC View 、模型和模型绑定(bind)器尽可能干净?

asp.net-mvc - .NET C# : How to handle forms authentication expiration during an AJAX call

c# - 无法通过 EF Core 找到 Oracle 表

c# - 从 "Code First From Database"EF6 更改生成的类

C# 正则表达式 : replace either of two matches with the same value

二进制文件中的 C# "hex shift"

c# - 如何在 Entity Framework 中用 Enum 替换 Int 属性?

c# - 在大型应用程序中使用 MVVM - 共享 View 模型、模型、状态等