开发者

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜