问题描述
||
我正在使用共享编辑器模板通过\'EditorFor \'HTML帮助器在视图中呈现嵌套集合,类似于以下内容。我不喜欢所有这些嵌套的部分视图,但这样做会适当地命名元素,以便它们可以无问题地发回到ViewModel中的控制器中。
如何在嵌套的最确定的级别对顺序进行排序?在这种情况下,如何使“ Budget.vbhtml”以年份顺序显示(降序)?
提前致谢!
顶级视图(Organization.vbhtml):
<div id=\"budgets\">
@Html.EditorFor(Function(org) org.OrganizationBudgets))
</div>
OrganizationBudget.vbhtml:
@ModelType CharityMVC.OrganizationBudget
@Html.EditorFor(Function(ob) ob.Budget)
Budget.vbhtml:
@ModelType CharityMVC.Budget
@Model.Year @Html.EditorFor(Function(b) b.Amount)
更新:
听起来当我填充Model对象时,应该在控制器中执行此操作,但是如何在linq查询中对子项或子项进行排序?这是我当前的代码:
Function Edit(ByVal id As Integer) As ActionResult
Dim o As Organization
Dim ovm As OrganizationViewModel
\'Load the organization from the database
o = (From org In _db.Organizations _
Where org.Id = id _
Select org).FirstOrDefault()
\'Map it to the ViewModel
ovm = AutoMapper.Mapper.Map(Of Organization,OrganizationViewModel)(o)
Return View(ovm)
End Function
解决方法
到目前为止,我最好的答案是:
多个LINQ查询填充子属性,如下所示:
Function Edit(ByVal id As Integer) As ActionResult
Dim o As Organization
Dim ovm As OrganizationViewModel
\'Load the organization from the database
o = (From org In _db.Organizations _
Where org.Id = id _
Select org).FirstOrDefault()
o.OrganizationBudgets = (From ob In _db.OrganizationBudgets _
Where ob.OrganizationId = o.Id _
Order By ob.Budget.Year Descending _
Select ob).ToList()
\'Map it to the ViewModel
ovm = AutoMapper.Mapper.Map(Of Organization,OrganizationViewModel)(o)
Return View(ovm)
End Function