Kev Hunter
Kev Hunter

Reputation: 2625

What is the best way in ASP.MVC2 to choose an image to display in the controller

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

Answers (2)

John Gietzen
John Gietzen

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

Tony Abrams
Tony Abrams

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

Related Questions