Transferring data to a layout page

I have a page layoutwith variables to fill out. Example:

@ModelType KarateAqua.schoolModel

<html>
    <body>

        @RenderBody()

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

I do not want to fill it in everyone ActionResult. Is there a way to transfer data to a page layoutonce and do it for all instances?

+5
source share
7 answers

OK, since you want this to be set as soon as you can use the partial view. However, depending on your needs, you will need to have a few partial views (maybe not ideal if the sections are scattered across the _layout page)

your partial view will look like

@model KarateAqua.schoolModel

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

controller

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

_layout.cshtml , ,

@Html.Action("Index","School")
+6

. , .

, . , ajax, , .

i, , 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;
                }
            }
        }
    }


}
+13

. , . , :

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

<html>
<body>

    @RenderBody()

    <div id="footer">
        <div class="content">
            <div class="bottom_logo">
                <a href="/"><span class="inv">@data.schoolName</span></a>
            </div>
        </div>
    </div>
</body>
</html>
+5

ViewBag ViewData .

<html>
<body>
@RenderBody()

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

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

, .

layout:

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

            @RenderBody()
//...
+1

HTTP Session -

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

_layout;

//persistent data   
<p>Hello, @Session["something"]!</p>

, , , .

+1
source

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";
        }
    }
}

The logic is simple: you create a BaseController that includes all the global parameters that you will use in the layout. (Like username or other data-based options)

You inherit (call) BaseControllerto get all the parameters in the current controller.

0
source

All Articles