Passing Data to a Layout Page

I have a layout page that has variables that need to be filled. Example:

@ModelType KarateAqua.schoolModel

<html>
    <body>

        @RenderBody()

        <div id="footer">
            <div class="content">
                <div class="bottom_logo">
                    <a href="/" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"><span class="inv">@Model.schoolName</span></a>
                </div>
            </div>
        </div>
    </body>
</html>

I don’t want to populate this in every ActionResult. Is there a way to pass data to a layout page once and do it for all instances?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Create an action filter and decorate your controller classes. Inside the action filter you have access to put values in the viewbag which are available to your layout.

This will run on each request and you will not have to set the values in each action. You can look for and ignore things like a child request and ajax requests which typically do not use the layout anyways and not set your viewbag values for those.

Below is a sample of an attribute i created to copy an object from the session and make it available to the layout via the ViewBag

public class CurrentUserAttribute : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        // Don't bother running this for child action or ajax requests
        if (!filterContext.IsChildAction && !filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
        {

            if (filterContext.HttpContext.Session != null)
            {
                var currentUser = filterContext.HttpContext.Session["CurrentUser"] as CurrentUser;
                if (currentUser != null)
                {
                    filterContext.Controller.ViewBag.CurrentUser = currentUser;
                }
            }
        }
    }


}

Method 2

OK since you want this to be set once you can make use of a partial view. However depending on your needs you will need to have several partial views (may be not ideal if sections are going to be scattered across the _layout page)

your partial view will look like

@model KarateAqua.schoolModel

<div class="bottom_logo">
<a href="/" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"><span class="inv">@Model.schoolName</span>
</div>

Controller
public class SchoolController : Controller
{
     public ActionResult Index()
     {
          //get schoolModel  
          return PartialView(schoolModel);
     }
}

in your _layout.cshtml place this line where you want to have the partial view to be inserted
@Html.Action("Index","School")

Method 3

You can open a code block on the layout page and fill the object there. This will execute every time the layout page is being used. The benefit is that you do not have to alter anything on your controller:

@{
    KarateAqua.schoolModel data = YourBusinessLayer.Method();
}

<html>
<body>

    @RenderBody()

    <div id="footer">
        <div class="content">
            <div class="bottom_logo">
                <a href="/" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"><span class="inv">@data.schoolName</span></a>
            </div>
        </div>
    </div>
</body>
</html>

Method 4

You could use ViewBag or ViewData to pass data to your Layout pages.

Layout

<html>
<body>
@RenderBody()

<div id="footer">
<div class="content">
<div class="bottom_logo">
<a href="/" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"><span class="inv">@ViewBag.schoolName</span>
</div></div></div>
</body>
</html>

Controller
public ActionResult Index(){
   ViewBag.schoolName = "Bayside Tigers";
   return View();
}

Method 5

Your Layout Page:

@ViewBag.LayoutVar

Your HomeController:
public class HomeController : BaseController
{
   //Here some logic...
}

Your BaseController
namespace ProjectName.Controllers
{
    public class BaseController : Controller
    {

        public YetkiController()
        {
            //This parameter is accessible from layout
            ViewBag.LayoutVar = "Suat";
        }
    }
}

Logic is easy: You create BaseController which includes every global parameters you will use in layout. (Like username or other data based parameters)

You inherit (call) BaseController to get all parameters into your current controller.

Method 6

You could always create action that returns a partial view of your header.

Just add this to your layout page:

<html>
    <head> 
    </head>
        <body>
            @{ Html.RenderAction("header", "MyController", new { area = "" }); }

            @RenderBody()
//...

Method 7

I used HTTP Session to persist the data between different pages –

//Opening page controller
public ActionResult Index()
{    
    Session["something"]="xxxx";
    return View();
}

In the shared _layout page;
//persistent data   
<p>Hello, @Session["something"]!</p>

Hope this helps but it won’t work if you start from a different page from the default one that has been set.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x