如果一个视图依赖它的控制器上? (ASP.NET MVC) [英] Should a view be dependent on its controller? (ASP.NET MVC)

查看:109
本文介绍了如果一个视图依赖它的控制器上? (ASP.NET MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关于这里asp.net mvc的设计/使用的问题。

Have a question about the design/usage of asp.net mvc here.

在HTML辅助类,你可以通过Htm​​l.ViewContext.Controller电流控制器。此外,你可以得到请求,路由集合以及更多从HTML辅助类。

In the html helper class, you can get to the current controller by Html.ViewContext.Controller. Moreover, you can get to the request, route collection and much more from the html helper class.

这是否违背了MVC的规则?难道这不是开辟了一条途径开发商做繁重的控制器依赖code的意见?

Doesn't this go against the rule of MVC? Doesn't this opens up a ways for developer to do heavy controller dependent code in views?

如果没有,什么是从HTML辅助类当前viewcontext和控制器的最佳实践的用例?

If not, what's the best practice use case for current viewcontext and controller from the html helper class?

先谢谢了。

推荐答案

使用一个强类型的ViewModel,所以你的观点仅依赖它,而不是谁产生它的控制器

Use a strong typed ViewModel, so your view is only dependant of it and not of the controller who generates it

这篇关于如果一个视图依赖它的控制器上? (ASP.NET MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆