tags:

views:

243

answers:

2

OK, I am now wondering how to handle navigation tabs with ASP.NET MVC. Giving an example, suppose you have the tabs like you have here at stackoverflow. So, Questions, Tags, Users, etc.

Now lets say you have a "sub tab" under this main one. So there were for example View and Add tabs displayed once you had selected the main Questions tab. Some questions:

  • Would it be best to have a set of routes like http://site/questions/view and http://site/questions/add for those two instances?

  • Would you therefore have a NavigationController that contained actions for each of the main tabs i.e. Questions, Tags, etc and then and id value for the sub tab i.e. View and Add. This would then give you something like the following:

public ActionResult Questions(string view)

public ActionResult Tags(string view)

Etc

  • Or would you have a controller per tab/navigation item and if so how would that be implemented?

  • Say you needed to show the tab(s) selected via something like highlighting. In the view (I guess you would have a partial view for this) for the navigation tabs, would this directly reference the URL to determine which should be highlighted or is this best acieved some other way?

Thanks in advance for any pointers

+2  A: 

Your best bet would be to stick to REST. I would stick to having a controller per main tab and each sub-tab corresponding to the possible REST actions: index, new. Edit and delete are item-specific so wouldn't get tabs. Create is called by new and update by edit.

An exception to that in your example would be the 'Ask a Question' tab. Its at the same level as the Questions tab (index) but would call Questions/New.

Brian
+1  A: 

I think your controllers should be more closely bound to your model than your user interface (see my answer to this question). In general, I think you should think of a controller handling input for your model, i.e., do something to my model, then return a view (the UI) that corresponds to that action. The elements of your UI could, but do not necessarily have to, reflect the model hierarchy. For example, Questions, Unanswered, and Ask a Question in SO all seem to relate to the question model, but they are all top-level interface elements. Unanswered also seems to have it's own controller, but could easily have been implmented as .../questions/unanswered rather than as .../unanswered.

tvanfosson
OK, but say your tabs were only a section of a site. For example, lets say it were a bank's site and you had various sections such as Credit Cards, Bank Accounts, Mortgages etc. Would you have a controller per logical section e.g. CreditCardController or a controller per main tab in that section?
Jon Archway
That depends on the underlying model and what the link is actually doing. For example, information for each may be handled by an information controller. For the account you would have an account controller, with actions for each account type/subaccount.
tvanfosson