将数据传递到布局页 [英] Passing Data to a Layout Page

查看:128
本文介绍了将数据传递到布局页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

余有布局页面具有需要被填充的变量。例如:

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>
</div></div></div>
</body>
</html>

我不希望这种填充在每一个的ActionResult。有没有办法将数据传递到布局页面一次,做到这一点对所有的实例?

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?

推荐答案

确定,因为你希望它设置一次,您可以利用局部视图。不过根据你的需要,你需要有一些局部视图(可能是不理想的,如果部分将要分散在_layout页)

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)

你的部分观点看起来像

@model KarateAqua.schoolModel

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

控制器

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

在你_layout.cshtml地方这条线,你想拥有的局部视图插入

in your _layout.cshtml place this line where you want to have the partial view to be inserted

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

这篇关于将数据传递到布局页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆