Can a controller affect the _layout.cshtml file?

I am stuck! I get the impression that the _layout.cshtml file is being used for content like MasterPage. Everything on every page. Naturally, I want to write code to display my sidebar menu in this file.

I want to dynamically display a list of categories from my database, but I had a problem with passing the actual category model to Layout.cshtml, as it seems that no controller affects it.

Any suggestions?

Otherwise, please tell me how to approach this problem. Over the past three days, I have been waving my brain and still have not been an elegant solution.

I need:

  • Dynamically retrieve a list of categories from the database.
  • Display this list of categories in each individual view. (Hence using _layout.cshtml)
  • Elegantly handle each of the different categories.

I'm crazy .: P How do you solve this?

+7
source share
4 answers

_layout.cshtml

@if(isSectionDefined("Categories")) { <div id="sidebar"> @RenderSection("Categories", required: false ) </div> } 

index.cshtml

 @section Categories { <ul> <li>Category One</li> <li>Category Two</li> <li>Category Three</li> </ul> } 

see this: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

+2
source

Any view model that you submit to your view is automatically available on your home page. If you are not using RenderAction / Action, which is the best approach, then you should create the necessary data for the main page in each action and add it to viewdata - either using a common base class for a strongly typed view model containing all the data on the main page or using dictionary viewdata.

I would highly recommend you go for the html.action approach. So you have a completely separate controller action to work with your list of categories. This action can retrieve neccesary category data and return the custom category list item as a partial view, and you don’t have to worry about polluting all your other actions with this data.

+1
source

As I see it, ViewData (and its relatives, such as ViewBag, Model, etc.) are for the current current view . Your _Layout.cshtml is not relevant to the current view; and it would be inconvenient if EVERY controller had to transmit category data in addition to all the other data that it needs to transmit for presentation.

Instead, I provide a static method in one of my helper classes that extracts categories from a database. I also do some caching there, so I don’t have to hit the DB with every single request. Then _Layout.cshtml just calls this static method. Simple and elegant.

If you want, you can bring it to a partial view, make it an auxiliary method, whatever.

However, one note - in my custom error view, the same _Layout.cshtml is also used, and if the database goes down, you get an exception trying to display the exception. ASP.NET MVC is smart enough to detect this and interrupt processing, but you left the default error page. What I did was to place try...catch around these dangerous calls that silently ignore the exception if the current page is the kind of error.

+1
source

I achieved something similar if my ViewModels implement an interface that has members that contain menu data. In my action method, I set this data. Then, in my opinion, I check if my view model implements this inteface, pulls out the menu data and displays the menu (in partial view)

0
source

All Articles