Problem with NVelocity, foreach and two Lists
Asked Answered
E

2

5

is there an easy way to solve the following problem.

Let's say I fetch a IList with some books in my controller from my model. Now I want to enrich the output and fetch a preview from Amazon with another model from an outside framework and get another IList.

Now I put both ILists into a property bag.

In NVelocity I use a #foreach for the BookList, but how can I access the amazonbooklist with the right preview? I cannot use $amazonbook[index], where index would be the isbn. Do I really need to put both lists in one big list with a simple onject containing only the two other objects?

Remember, both models are from different frameworks and cannot be placed in one framework. Both frameworks have to stay seperated. I try to solve the NVelocity problem and ofcourse, this problem is just an example, we don't sell books ;)

Exhort answered 24/11, 2008 at 21:9 Comment(0)
R
1

There may be a simpler solution, but I would create a third class like such

class BookList{
       MyBookObject     a;
       AmazonBookObject b;
}

a list of this third class could be passed to your view at once.

Rosaline answered 24/11, 2008 at 21:39 Comment(2)
So, this is part of the controller or should I create another model for this data gathering?Exhort
I would say that's up to you - you could do this using Linq and an anonymous type if you didn't want a class hanging around, but that limits your ease of testability a little. Using a model would be my approach and delegating this functionality to a separate service to keep the controller light.Kedron
W
6

Look here: http://www.castleproject.org/monorail/faq.html#NVelocity%20View%20Engine

You might access a dictionary in NVelocity via

$amazonbook.get_Item($index) 
Wyman answered 8/12, 2008 at 11:38 Comment(0)
R
1

There may be a simpler solution, but I would create a third class like such

class BookList{
       MyBookObject     a;
       AmazonBookObject b;
}

a list of this third class could be passed to your view at once.

Rosaline answered 24/11, 2008 at 21:39 Comment(2)
So, this is part of the controller or should I create another model for this data gathering?Exhort
I would say that's up to you - you could do this using Linq and an anonymous type if you didn't want a class hanging around, but that limits your ease of testability a little. Using a model would be my approach and delegating this functionality to a separate service to keep the controller light.Kedron

© 2022 - 2024 — McMap. All rights reserved.