在局部视图修改MVC 3 ViewBag不会坚持到_Layout.cshtml [英] Modifying MVC 3 ViewBag in a partial view does not persist to the _Layout.cshtml
问题描述
我使用MVC 3用的Razor视图引擎。我想设置一个局部视图内的ViewBag一些值,并希望在我_Layout.cshtml检索这些值。例如,当您设置一个默认的ASP.NET MVC 3项目你在/查看/共享文件夹中的文件_Layout.cshtml。在_Layout.cshtml页面标题设置是这样的:
I am using MVC 3 with the Razor view engine. I want to set some values in the ViewBag inside a Partial View and want retrieve those values in my _Layout.cshtml. For example, when you setup a default ASP.NET MVC 3 project you get a _Layout.cshtml file in the "/Views/Shared" folder. In that _Layout.cshtml the Page Title is set like this:
<title>@ViewBag.PageTitle</title>
然后在/Views/Home/About.cshtml查看ViewBag的内容被修改:
Then in "/Views/Home/About.cshtml" view the contents of the ViewBag are modified:
@{
ViewBag.Title = "About Us";
}
这工作正常。当关于视图呈现在页面的标题是关于我们。所以,现在我想渲染我关于视图内的局部视图,我想修改我ViewBag.Title局部视图中。 (/Views/Shared/SomePartial.cshtml)
This works fine. When the About view is rendered the page title is "About Us". So, now I want to render a Partial view inside my About view and I want to modify the ViewBag.Title inside my Partial view. ("/Views/Shared/SomePartial.cshtml")
@Html.Partial("SomePartial")
在此局部视图我有这样的code:
In this Partial view I have this code:
@{
ViewBag.Title = "About Us From The Partial View";
}
当我调试这个code我看到ViewBag.Title被置为关于我们,然后在局部视图我看到它得到重置为关于我们从局部视图,但当code打_Layout.cshtml它可以追溯到关于我们。
When I debug this code I see the ViewBag.Title get set to "About Us" and then in the Partial view I see it get reset to "About Us From The Partial View", but when the code hits the _Layout.cshtml it goes back to "About Us".
这是否意味着,如果ViewBag的内容在一个局部视图被修改,将不会出现这些变化(可访问),在主视图(About.cshtml)或_Layout.cshtml?
Does this mean that if the contents of the ViewBag are modified in a Partial view, those changes will not appear(be accessible) in the main view (About.cshtml) or the _Layout.cshtml?
在此先感谢!
推荐答案
我也有这个问题,找不到任何整洁,明显的解决方案。
I also had this problem, and couldn't find any neat and obvious solution.
我想出了解决的办法是实施返回您定义的PageData类的HTML扩展方法,包含任何你需要的数据:
The solution I came up with was to implement an Html extension method that returns a 'PageData' class that you define, containing whatever data you need:
[ThreadStatic]
private static ControllerBase pageDataController;
[ThreadStatic]
private static PageData pageData;
public static PageData GetPageData(this HtmlHelper html) {
ControllerBase controller = html.ViewContext.Controller;
while (controller.ControllerContext.IsChildAction) {
controller = controller.ControllerContext.ParentActionViewContext.Controller;
}
if (pageDataController == controller) {
return pageData;
} else {
pageDataController = controller;
pageData = new PageData();
return pageData;
}
}
有认定为当前请求顶层控制器,每个所述方法被调用同一HTTP请求之内的时间返回相同PageData对象。它创建了一个新的对象PageData第一次它被称为一个新的HTTP请求。
It finds the top-level controller for the current request, and returns the same PageData object every time the method is called within the same HTTP request. It creates a new PageData object the first time it is called in a new HTTP request.
这篇关于在局部视图修改MVC 3 ViewBag不会坚持到_Layout.cshtml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!