1

I have 2 controllers: 1 top level controller,MainController, and a SubController, which MainController has an instance of and controls. The SubController has a Subview which goes inside the MainView as some sub web element.

What is a good way to insert the SubView into MainView? I can think of:

1) I can give SubController and SubView a reference of the MainView and append to it in the Subview

2) Return the html from the SubView rendering function to SubController, which returns to it to the MainController -> MainView -> render

Or is there a much better way?

2
  • When you are saying a SubView, do you mean a PartialView? What version of MVC are you using? Commented Dec 23, 2014 at 22:35
  • 1
    @JenniferS I'm not using any framework for MVC. Just using javascript and making from scratch. Maybe SubView is the wrong word. For example, I'll have Mainview render a menu and page template for swapping between graph visualizations. I'd have each graph visualization as a Subcontroller and subview. Commented Dec 23, 2014 at 23:35

1 Answer 1

1

For the cleanest separation between the Main and Sub View/Controller, the SubView/SubController combination should be unaware of the existence of the MainView and MainController. That way you leave open the possibility to re-use the SubView/SubController elsewhere in the application.

There are different ways to do the rendering of sub-views, each with advantages and disadvantages.

The most important thing to keep in mind is that if you are generating your HTML on the server, then the View components should not know (nor care) if their output is sent directly to the client's browser or if it will be embedded in a larger view.
You could also say that every View is ultimately embedded within a master-View that only tags on the HTML header and body elements.

Then you can either let the Controller components render their sub-Views or you can do that in the View components.
My preference is to do it in the View, as it is all display related, which is the responsibility of the View components of MVC. On the other hand, that results in quite a bit of programming logic in the View, which many are not really comfortable with and which is not that well supported by the templating engines that are commonly used for generating views.

1
  • Is there any more strait answer with JavaScript examples? I also have this problem. I develop datepicker and timepicker components with ability to display both bound together with one container frame and one OK button. This container frame and button are part of my MainView and events notified from it are handled by MainController. DatePicker and TimePicker both has their Controllers and Views - I would name them as SubControllers and SubViews. Please support with examples of embedding SubView in MainView Commented Jan 27, 2016 at 17:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.