1

Shouldn't a controller in MVC be completely ignorant of how the data it gives to the view will be displayed?

My question relates to the SelectList. Should the controller be clever enough to know that the data will be presented in a drop down? Or should it simply give the view, through the view model, a list of items, and let the view handle it however it needs?

Paul
  • 1,519
  • 1
  • 16
  • 26

2 Answers2

1

Absolutely - the controller is responsible for passing the correct data in the correct structure, but doesn't give a monkeys about how it looks. The view could choose to display each list item in a random location if it wanted to - the controller shouldn't be involved in visual logic. Otherwise, as you say, you lose the 'separation of concerns'.

chrisfrancis27
  • 4,516
  • 1
  • 24
  • 32
  • are you agreeing with the first paragraph, or the second? :). I personally think that the controller making an assumption about how the info will be displayed (in a select list) is not separated enough. I understand that the controller needs to provide what the view needs, but that doesn't stretch to knowing how it will use it. Defining data as a SelectList is knowing too much in my opinion. Not that the SelectList isn't useful, I'm just wondering if it is strictly MVC. – Paul May 13 '11 at 16:02
  • @Paul - I agree with the first paragraph! :) Yeah, if a controller gets too 'intelligent' with what the view needs it is very easy (I know I've done this too often) to get carried away and essentially remove the rendering responsibility from the view. Usually, creating a strongly-typed ViewModel and populating that from the controller then passing it to the view is a clean way to go, and abstracts any render-specific logic from the controller. – chrisfrancis27 May 16 '11 at 09:43
1

I see what you are getting at. It just depends on how you look at the SelectList. The select list is really just a data structure optimized for this specific use. You could actually use the SelectList for something else if you wanted. In this sense, there is still a clear separation of concerns.

AlbertVo
  • 772
  • 4
  • 9
  • Agreed, the SelectList is just a data structure, but by its very nature, it is built for a specific purpose, in that it has a Text value, and a Value value. That shouts "I'm going straight into a drop down!". And again, I'm not denying its usefulness, just curious about a pure MVC separation. Thanks for the comment. – Paul May 13 '11 at 16:05