40

I have a shared view in my _Layout.cshtml for my header named "_Header.cshtml".

I would like to display text and image from the database, so I need my controller to go in the database and return it to the _Header.cshtml.

How can I do that because the controller called is always different each page the user goes. Is there a way to have controller with Shared View?

Here is the _Layout.cshtml

    <div id="header">
        <div id="title">
            @Html.Partial("_Header")
        </div>

        <div id="logindisplay">
           @Html.Partial("_CultureChooser")
            <br />
           @Html.Partial("_LogOnPartial")
        </div>

        <div id="menucontainer">
           @Html.Partial( "_MenuPartial")
        </div>
    </div>

    <div id="main">
        @RenderBody()
        <div id="footer">
        </div>
    </div>

</div>

Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341

5 Answers5

60

In your contoller action you could specify the name of the view:

public class MenuController : Controller
{
    [ChildActionOnly]
    public ActionResult Header()
    {
        var model = ... // go to the database and fetch a model
        return View("~/Views/Shared/_Header.cshtml", model);
    }
}

Now in your _Layout.cshtml instead of @Html.Partial("_Header") do this:

@Html.Action("Header", "Menu")
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I added the _Layout.cshtml in the question. It's not on a click, it's everywhere in the website. – Patrick Desjardins May 09 '11 at 15:15
  • 1
    @Daok, what do you mean by a *click*? You could use the `@Html.Action` helper method in your layout which will call the child action on the corresponding controller. This is more powerful than a simple `Html.Partial` as here you have the possibility to define a separate controller/model and view for this section of the site which is repeated everywhere and which is independent from the main controller. You may checkout the following blog post about child actions: http://haacked.com/archive/2009/11/18/aspnetmvc2-render-action.aspx – Darin Dimitrov May 09 '11 at 15:16
  • Thank you, I still need to learn MVC :P I have this error now : Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'. – Patrick Desjardins May 09 '11 at 15:24
  • 3
    Now it works, I was returning a View() instead of a PartialView() – Patrick Desjardins May 09 '11 at 15:31
  • @DarinDimitrov I faced to this error: {Insufficient stack to continue executing the program safely. This can happen from having too many functions on the call stack or function on the stack using too much stack space}, What can I do? – QMaster Nov 04 '16 at 20:33
  • @DarinDimitrov I used vidalsasoon point and that is solved, Thanks man. – QMaster Nov 04 '16 at 20:44
  • Also, don't forget to add `@{Layout = null;}` at top of partial view which you want to include in `_layout.cshtml`. Otherwise, it could give the `System.StackOverflow` exception. – Zeeshan Ahmad Khalil Jul 11 '20 at 11:15
11

... 1 year later would just like to add one thing to Dimitrov answer. You can make the controller a little cleaner:

public class MenuController : Controller
{
    [ChildActionOnly]
    public ActionResult Header()
    {
        var model = ... // go to the database and fetch a model
        return Partial("_Header", model);
    }
}
vidalsasoon
  • 4,365
  • 1
  • 32
  • 40
  • 2
    You saved me, Just I think it must be { return PartialView("_Header", model); } I didn't find Partial method, Am i wrong? – QMaster Nov 04 '16 at 20:43
2

Create an action in one of your controllers to render the header view, then simply call @Html.RenderAction("Header") within the _Layout.cshtml.

You can also pass a model into the RenderAction method if required.

William
  • 8,007
  • 5
  • 39
  • 43
0

I hope the question you have asked is Like.... Can we have a controller for a Shared Layout View.

Simple answer is No.

To achieve this goal you have to create a partial view for the Same purpose and put it into you shared Layout. By that means you can achieve you Goal

Sanu Antony
  • 364
  • 4
  • 15
0

While the RenderAction approach that WDuffy provided works well, I recently blogged about this very topic using another approach using IoC:

http://crazorsharp.blogspot.com/2011/03/master-page-model-in-aspnet-mvc-3-using.html

Community
  • 1
  • 1
BFree
  • 102,548
  • 21
  • 159
  • 201