Partial with login and page logic - asp.net-mvc

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.

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

Both create and list in the same view MVC4?

I am new to MVC4. I know basic CRUD functionality in MVC, but how can I achieve both create and list in the same view? Like after a user creates data the user will automatically be redirected to the same view and view the list of data.
You may use partial views to accomplish this. Create the create / edit pages the same way as you do and below the form you may render a partial view that would render the list with even edit / delete buttons.
Alternatively, you may pass both the form data (in case of edit) and the list data to a view to render there.
I would suggest you to use the partial views approach as it would simplify the view and the partial view can be reused on some other page also.
Please take this as a starting point and not as a follow-it-blindly solution.

ASP.NET MVC loading multiple partial views into a single div using JQuery

I am working on a help page which is composed of a navigation tree, content box, and search box. The navigation tree has links to Frequently Asked Questions and Glossary, each of which are linked to an Action which return partial views.
To the right of the navigation tree I have a single content div that I would like to contain whichever partial view is selected in the navigation tree.
Success case (what I want to accomplish): clicking on one of the FAQ links calls the FAQ() method in my controller, then returns a partial view which is then displayed in my content div to the right of the navigation tree. Clicking on another item would cause another partial view to be loaded.
How do I go about this? I've read a ton of ASP.NET MVC JQuery loading blog posts and tutorials but can't find anyone who's done exactly this.
Many thanks!
You should be able to use the jQuery method .load() to load HTML into your div.
http://api.jquery.com/load/
You can create an action that returns the partial view as HTML.
ASP.NET MVC Returning Partial View as a full View Page
jQuery:
one easy way you can do is load all partial views in "Container Div" at page load in one go (if performance is not a issue)
then assign each partial div with different div id inside "container", than use jquery to control show(); hide(); for each div.
MVC:
however if i were you, "Glossary" and "FAQ" looks same model to me it shouldn't be put in different partial view in first place.
if they did designed in separate model, in this scenario, i would recommend you to create a proxy class as a ViewModel above models you want to display, and then load it with one partial view only

ViewData not inheriting in partials

I was trying to use a shared partial view to render when a particular listing page has no data. I wanted to use ViewData to pass information from the page into my listing control, which would then conditionally render the NoData partial view using the ViewData values.
I would like to be able to specify them in the view markup, not in the controller action, but when I add them in the view the don't seem to inherit down into child partial views (like the Nodata partial view). However, specifying them in the ViewData values in the controller actions works fine, the data is available all the way down...
Does anyone know why it behaves this way?
When rendering a partial you can also pass the ViewData.
<% Html.RenderPartial("NoData", ViewData); %>
<%Html.RenderPartial("partialViewName", "viewData", "model"); %>
it is a best practice to do the decision inside the controller, if you have a scenario to make a decision inside the view, separate them and call them inside the controller conditionally

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