MVC 选择列表关注点分离
MVC 中的控制器难道不应该完全不知道它提供给视图的数据将如何显示吗?
我的问题与 SelectList 有关。控制器是否应该足够聪明才能知道数据将在下拉列表中呈现?或者它应该简单地通过视图模型为视图提供一个项目列表,并让视图根据需要处理它?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绝对 - 控制器负责以正确的结构传递正确的数据,但不会告诉猴子它的外观。如果愿意,视图可以选择在随机位置显示每个列表项 - 控制器不应参与视觉逻辑。否则,正如您所说,您将失去“关注点分离”。
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'.
我明白你的意思了。这仅取决于您如何看待 SelectList。选择列表实际上只是针对此特定用途优化的数据结构。如果您愿意,您实际上可以将 SelectList 用于其他用途。从这个意义上说,仍然存在明显的关注点分离。
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.