Can a controller influence the

I'm stuck! I'm under the impression that the _layout.cshtml file is used for MasterPage-like content. Everything there is rendered on every page. Naturally, I want to write the code for rendering my sidebar menu in that file.

I want to dynamically display a list of Categories from my DB, but I'm having a problem with passing the actual model of categories to Layout.cshtml since it seems no controller actually touches it.

Any suggestions?

Otherwise please tell me how to approach this problem. I've been wracking my brain for the past three days and still no elegant solution.

I need to:

  • Dynamically fetch a list of Categories from the DB.
  • Display this list of Categories on every single view. (Hence the use of _layout.cshtml)
  • Elegantly handle each different categories click.
  • I'm at my wits end. :P How would you solve this?


    _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


    Any viewmodel that you pass to your view is automatically available within your master page. If you do not use RenderAction/Action which is the best approach, then you must create the necessary master page data in every action and add it to viewdata - either by having a common base class for your strongly typed viewmodel that contains all master page data or by using the viewdata dictionary.

    I would strongly recommend that you go down the html.action approach though. In this way, you have a totally separate controller action for dealing with your list of categories. This action can retrieve the neccesary category data and return the categorylist usercontrol as a partialview and you will not have to worry about polluting all your other actions with this data.


    As I see it, ViewData (and its relatives like ViewBag, Model, etc.) is meant for the specific current view . Your _Layout.cshtml is not specific to the current view; and it would be awkward if EVERY controller would have to pass the categories data in addition to whatever else data it needs to pass for the view.

    Instead, what I do, is provide a static method in one of my helper classes that retrieves the categories from the DB. I also do some caching there, so that I do not have to hit the DB on every single request. The _Layout.cshtml then simply calls this static method. Simple and elegant.

    If you wish, you can bring this out to a partial view, make it a helper method, whatever.

    One note of caution though - my custom error view also uses the same _Layout.cshtml, and if the DB goes down, you get an exception trying to display the exception. ASP.NET MVC is smart enough to detect this and abort processing, but you're left with a nondescript default error page. What I did was to place try...catch statements around these dangerous calls, which quietly ignore the exception if the current page is the error view.

    链接地址: http://www.djcxy.com/p/49426.html

    上一篇: Rails:追溯添加测试的好过程?

    下一篇: 控制者是否可以影响这个