控制器有什么责任以某种格式提供视图数据?
我有一个控制器,需要通过 Ajax + Json 将信息返回到视图......到目前为止相当标准。但是显示数据的组件要求它以非常特定的结构传递。
我不愿意开始在这样一个特定的庄园中将数据吐回视图,以防我稍后需要更换该组件。这意味着控制器和视图的更改,而不仅仅是视图。
将控制器发送到客户端的视图的数据转换为一个更好的主意吗?或者我是否正在对视图和控制器之间的分离进行分析,并且应该硬着头皮在控制器中完成这一切?
谢谢,
基隆
I've a controller that needs to return information to a View via Ajax + Json...fairly standard so far. But the component displaying the data requires it to be delivered in a very specific structure.
I'm loathed to start spitting back data to the view in such a specific manor in case I need to swap out that component later. It would mean changes to the controller and the view, not just the view.
Would it be a better idea to translate the data the controller is sending the view on the client side? Or am I being to anal about the seperation between view and controller, and should just bite the bullet and do it all in the controller?
Thanks,
Kieron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 ViewModel 类中进行这些类型的转换。我假设您在强类型视图中使用 ViewModel 绑定而不是直接域对象绑定。我认为这正是 ViewModel 类的用途。
然后,您始终可以在这些 ViewModel 中拥有转换器函数并在视图中调用它们。
I do these kind of conversions in the ViewModel class. I assume you are using ViewModel binding rather than direct Domain object binding in the strongly typed Views. I think that's exactly what ViewModel classes are for.
Then you can always the have converter functions inside those ViewModel and call them in the view.