Reputation: 2625
I am playing around with an ASP.MVC site, I want one of three images to be displayed depending on the value of an enum in the strongly typed model I have.
I could use an IF/Case statement in the view but it should be the responsibility of the controller I feel, what's the best way to implement this?
Upvotes: 3
Views: 224
Reputation: 49544
If you have an Enumerable<YourModel>
, You may want to provide a Dictionary<YourEnum, Uri>
from the controller to the view.
That way, the controller can decide what valid images there are, and etc.
However, choosing an image seems like a fundamentally view-bound activity. As an exercise, imagine how you would handle this if the controller we actually driving a WinForms app rather than a web app.
If it were me, I would use a Dictionary<YourEnum, string>
, where the values in the dictionary were the names of the images. I would then use URL routing to choose where the browser should pick up the images.
Upvotes: 2
Reputation: 4673
I would just have the controller pick an image and then pass the image to the view. On the view just use the passed value to render out the image.
Upvotes: 1