使用EditorFor对嵌套集合进行排序

时间:2011-05-26 18:40:57

标签: asp.net-mvc-3 razor editorfor

我使用共享编辑器模板在我的视图中使用'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

1 个答案:

答案 0 :(得分:0)

到目前为止我的最佳答案:多个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