0

I am working on a WinForms application with the MVC pattern. Should I access UI components from the controller, for example a treeview node?

I want to access a treeview selected node and change its tag and something like that, should I access it from controller or is it against the principle of MVC?

The only thing which is irritating me is that the treenode belongs to the Windows.Forms namespace and from an MVC perspective view/form related component should be used in views only so that's why I'm asking.

user247702
  • 23,641
  • 15
  • 110
  • 157
Numan Hanif
  • 246
  • 2
  • 17

2 Answers2

2

No, you should not control the behaviour of the view in the controller. You should pass data to the view, and the view should decide to adapt based on the data it received.

If you want to react on things happening in the view, for example changing the selected tree node, you should send data back to the controller. The controller may then respond with additional data, which your view can display.

user247702
  • 23,641
  • 15
  • 110
  • 157
  • and what about getting data from treeview selected node like text/name etc ... is it also against mvc rules ??? – Numan Hanif Oct 08 '14 at 11:19
  • The view should send data back to the controller. For example, when changing the selected treenode, you may want to send to the controller which node is selected, and the controller may send additional data back to your view, which your view can then choose to display. – user247702 Oct 08 '14 at 11:21
  • let me clear it, i have binded treeview events with controller, when any event trigger on treeview the controller has to know about the selected node and data about that tree node, so i have to access the selected node for information like node name/text etc for further bussiness logic – Numan Hanif Oct 08 '14 at 11:24
  • Admittedly I have no experience with MVC in WinForms, only in ASP.NET MVC, but event binding should be a part of the view, not of the controller. A form, with its pre-generated code and the code you write yourself, is a view. – user247702 Oct 08 '14 at 11:27
1

Access the UI components from the controller through calls to the view not directly. Imagine changing the treeview to something else, say a Telerik based treeview later on, should you controller also have to change its code when that happens? However for winforms I believe MVP would be applicable and goes well with the comments I am making and what Stijn said. Maybe look into the difference between the two.

MVC vs MVP

Community
  • 1
  • 1
Thulani Chivandikwa
  • 3,402
  • 30
  • 33