在局部视图中修改 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 项目时,您会在/Views/Shared"文件夹中获得一个 _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";
}
这很好用.当关于"视图呈现时,页面标题是关于我们".所以,现在我想在我的 About 视图中渲染一个 Partial 视图,我想修改我的 Partial 视图中的 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")
在这个局部视图中,我有这个代码:
In this Partial view I have this code:
@{
ViewBag.Title = "About Us From The Partial View";
}
当我调试这段代码时,我看到 ViewBag.Title 被设置为关于我们",然后在部分视图中我看到它被重置为关于我们来自部分视图",但是当代码点击 _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".
这是否意味着如果在 Partial 视图中修改了 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.
我想出的解决方案是实现一个 Html 扩展方法,该方法返回您定义的PageData"类,其中包含您需要的任何数据:
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 对象.它在新的 HTTP 请求中第一次被调用时创建一个新的 PageData 对象.
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屋!