I am using shared editor templates to render nested collections in my view with the ‘EditorFor’ HTML helper, similar to the following. I’m not in love with all of these nested partial views but doing it this way names the elements appropriately so that they post back to my controller in the ViewModel without an issue.
How would I sort the order at the most resolute level of the nest? In this case, how would I get “Budget.vbhtml” to display in Year order (descending)?
Thanks in advance!
Top-level view (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)
UPDATE:
It sounds like I should be doing this in my controller when I populate my Model object, but how do I sort the children or children-of-children in a linq query? This is my current code:
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
My best answer so far:
Multple LINQ queries populating the child properties like so: