Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a view be dependent on its controller? (ASP.NET MVC)

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

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.

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?

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

Thanks in advance.

like image 757
Herman Avatar asked Dec 18 '22 05:12

Herman


1 Answers

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

like image 101
Gregoire Avatar answered Dec 28 '22 05:12

Gregoire