MVC Navigation Tabs - asp.net-mvc

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

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.

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.

Related

Populate Menu Bar dynamically in MVC

I have my category name and sub category names stored in a table.
I want to have a layout page(master page) which has a menu bar. I want to pull the category names and subcategory names from the table and display it as a menu and sub menus in my layout page.
How can this be achieved?
The best way to do that, as far as Im aware, is to use the ViewData. You can do this on every action call (which is not only wrong but also a nightmare) or you can create an abstraction for your controllers and inherit it for every controller. Then in the abstraction itself you will make the database call in the constructor and make sure it is loaded for every action call. See this for more info

Partial with login and page logic

I have a navigation bar that I want to be a partial. The contents of the nav bar will vary slightly, like if the user is logged in or if you're on a certain page you might get an extra link.
How do I best deal with providing the data to the partial? Should I pass this in ViewData for every controller?
A list of options would help me the most, because likely I will have to utilize a few different techniques.
Make a model for it. Create a class NavModel which in its constructor gathers all the data it represents (friendly user name, current page, etc.) and publishes them as public properties. Then just bind the model to the view as usual. Oh, and avoid instantiating nav model directly in a view, instead create a controller action (called Nav) which instantiates the NavModel and returns the nav partial view. Mark that action as ChildActionOnlyAttribute so that it can't be requested by the client. Then use #Html.RenderAction in the view that needs to render the nav partial (usually a layout view).
I had the same problem sometime ago, when i have to show some menu to admin and some menu to super admin and some to others... What i did in that case. I made a an action returning partial view and rendered it on master page. The view was strongly typed. ( A class whose property representing roles of user.) so using that strongly typed class i wrote if and else if logic in my razor view..and that solved my problem....Hope this would help you.

Wrap section and main body into one form in ASP.NET MVC 3

In my ASP.NET MVC 3 project I have a master layout with a section defined. This section is responsible for displaying content in a sidebar, when it has any content assigned to it.
The problem I've encountered is the following:
I have some edit views, where both the sidebar and the main area is used for editing data. In this case the sidebar and the main body should be wrapped into one single form with a single submit button.
What is the best solution for this? The solution I came up with is that when the functionality I mentioned is necessary, I set a boolean property in my ViewBag. If this property is true, the master layout is rendered with the sidebar and the main content area wrapped in a form.
Is there a better way to this? The solution I described is a bit 'hackish' for me.
I would have two layouts. One with a seperate side-bar, and one without. Then, in the pages you need a sidebar with editable fields, you include the sidebar in your content page, not in the master.
The boolean you are using in the ViewBag can be better represented in the Model for the view of the master page. You can then include the side bar using a partial view. It would look somehting like this...
master.cshtml:
#using (Html.BeginForm("ActionName", "Controller", "POST")){
//
// Master form elements go here
//
//Side bar
#if(Model.ShowSideBar){
#Html.Partial("MySideBarPartialView" [, Model.SideBarModel ] )
#}
#}
You model (if any) should have a property that is set with the model for the side bar.

How to build a tabbed Edit View for a big ViewModel in ASP.NET without JavaScript?

I've got a big ViewModel for a ContactViewModel with several addresses (default, invoice, delivery). This ContactViewModel I would like to edit within a DefaultAddress tab, etc. and I would like to know how to handle this without JavaScript? Is this possible?
Tell me if I'm off base here;
The way i think i'd approach this is to create a partial view which takes a list. the partial view would itterate through the list and create another partial view which is the tab.
on click of the tab i'd do a postback and store the clicked tab. this id then becomes the active tab.
when i come back to rebuild my page, the partialview for the actual tab would need to check to see if it's active and then make itself visible. if not visible then simply render nothing maybe.
This can be done with CSS. Here is an example: http://www.alistapart.com/articles/slidingdoors/
The selected tab/view will need to be rendered on the server. I can see each tab being a link, when the link is clicked the correct view and selected tab is returned.
Some of the css tabs don't work correctly in IE6. I'm not sure if the above link is one of them.

asp.net mvc 2 menus between 4 controllers

I want to have 2 navigation menus- One will be a "Top Level" menu, with 4 choices, each pointing to the index of a separate controller.
I would like to have a "Controller-Level" sub-menu on the left of my screen. This will correspond to links relevant to the controller selected in the top menu.
The "controller-level" menu is not static and needs to be customized based on roles of user.
The top-level one is basic. But how can i create the second menu that will change when a controller is selected from top?
danke!
I'm working on a similar situation where I need two menus. Was a "best-practice" ever resolved here? I have the need for one menu on the left, and a dynamically created tab-like menu at the top depending on what page I'm working with/on.
The user actually needs the ability to add/remove/hide top tabs depending on preferences. We are using MVC 2 and I'm not sure if the tab bar belongs in a control, on individual views, or what...and also how to make them both interact with my pages. (The left nav needs to show where the user is at all times, and the top tab needs to be highlighted based on the sub-page the user has selected.
If you are using MVC 2 RC or MVC Futures, then use RenderAction.
Create a controller for the dynamic menu, and call RenderAction("action", "controller"). This will invoke the controller, and you can have your logic in the controller to show the correct menu.

Resources