Passing Data to a Layout Page

2019-01-19 01:57发布

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="/"><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?

7条回答
一纸荒年 Trace。
2楼-- · 2019-01-19 02:57

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.

查看更多
登录 后发表回答