Say i have a List with items
-17″ Screen
– 100GB HD
-10788 Firewire
-Lock Cable
-Monitor
-Mouse
-Keyboard
– USB
I want to iterate a list for items starting from A to c, D to F and so on…
I want this to run for items starting from A to items starting C
@foreach (var item in Model.Items.OrderBy(i => i.Title))
{
// Code
}
I want this to run for items starting from D to items starting F
@foreach (var item in Model.Items.OrderBy(i => i.Title))
{
// Code
}
Any Help ?
I would start by defining a view model (as always):
then a controller action that will convert fetch the model from somewhere and then map it to a view model.
Remark: in this example I will put the mapping code between the model and the view model inside the controller action but normally this should go into a separate mapping layer. For example if you use AutoMapper that could be a great place.
So:
and now all that’s left in the corresponding view is to display the titles according to the requirements:
and the result:
And after refactoring the mapping logic into a mapping layer here’s how the corresponding controller action might look like:
Clean and dry.