Populate Menu Bar dynamically in MVC - asp.net-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

Related

Recommended way to do the search and populating the grid in ASP.NET Core MVC 6

I am creating a project where I need to create a view with search textboxes and a dropdown which would be populated from the server. Same view also have a grid which would be populated based on the search criteria enter/selected by user.
I want to know what would be the design of the page in terms of showing both on same page. Should I create a partial view for the grid or Search panel or add both in the single view?
Note that dropdown list would need to be populated from the ViewModel. So what is the common practice in the situation. I am new to this I have done few pages but with lot of code, session and ViewBags and I think I am not following recommended practice.
Actually, the design for your web application is according to your requirement.
For example, if you want grid to work with other datasource or view, you could build it as a component, then if you want to use it, you could directly use this component to avoid write the same codes for multiple time.
If you want to use same grid to show in some other page, you could build it a partial view, then you could directly call this view to show something.

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.

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.

MVC Navigation Tabs

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.

Resources