更新:由于这是不是一个MVC4网站,但独立的剃须刀模板,为MVC标准答案不适用。这个页面展示如何通过使用PageData对象Customizing Site-Wide Behavior
我原来的答复并不真正适用的页面之间的数据,但其概念仍然是有效的:
有可用于完成至少四个对象这个。也许最简单的是ViewBag。有关更多细节,请参阅When to use ViewBag, ViewData...。
@{
ViewBag.topNav = "services";
}
您可能会发现有点问题,因为ViewBag不提供强类型属性。如果ViewBag.topNav不存在,它将在访问时返回null。
但是,文章中的容器都不会提供类型检查。这使我能够处理这个问题的第四个对象。这是传递给视图的模型对象。有关更多详细信息,请参见Using ViewModels。
public class IndexViewModel {
public string TopNav { get; set; }
// other view properties here
}
在你的控制器:
public ActionResult Index() {
var model = new IndexViewModel {
TopNav = "services",
// set other properties here
}
return View(model);
}
和索引视图:
@model IndexViewModel
@RenderPage("~/Shared/_Header.cshtml",Model)
_Header.cshtml:
@model IndexViewModel
<h2>@Model.topNav</h2>
一个要注意的最后一个项目是考虑使用Shared Layout.cshtml而不是嵌入标头每页中的页脚。
来源
2014-07-08 22:01:14
B2K
使用[ViewBag](http://www.codeproject.com/Articles/476967/WhatplusisplusViewData-cplusViewBagplusandplusTem)或强类型模型。 – pasty
@pasty - 我不会推荐使用ViewBag,因为它没有强类型。 – Dangerous
@xweb - 通过将“服务”一词传递给页眉或页脚部分,您试图实现的是什么? – Dangerous