从PartialView设置页面标题 [英] Set a page title from a PartialView

查看:103
本文介绍了从PartialView设置页面标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从PartialView

I'm trying to set a pageTitle from a PartialView

下面是我的code:

PartialView模板:

@{
    ((WebViewPage)WebPageContext.Current.Page).ViewBag.Title = "Page title";
}

_Layout.cshtml:

<title> @ViewBag.Title</title>

如何做到这一点?

How to do this?

推荐答案

这不是一件部分应该做的。它只是没有责任修改父视图 ViewBag 。而除了这是不可能的。 <一href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.html.renderpartialextensions.renderpartial.aspx\">Quote从MSDN :

That's not something that a partial should do. It's just not its responsibility to modify the parent view ViewBag. And in addition to that it's not possible. Quote from MSDN:

在一个局部视图实例化时,获得自己的副本
  的ViewDataDictionary对象是提供给父视图。该
  因此局部视图访问父视图中的数据。
  然而,如果局部视图更新数据,这些更新影响
  只有局部视图的ViewData的对象。父视图的数据不
  改变了。

When a partial view is instantiated, it gets its own copy of the ViewDataDictionary object that is available to the parent view. The partial view therefore has access to the data of the parent view. However, if the partial view updates the data, those updates affect only the partial view's ViewData object. The parent view's data is not changed.

您总是可以做到的解决方法的<一个href=\"http://stackoverflow.com/questions/4974027/modifying-mvc-3-viewbag-in-a-partial-view-does-not-persist-to-the-layout-cshtml\">shown这里但请不要。谐音不应该设置任何父状态。他们不应该知道的东西父

You could always do the workarounds shown here but please don't. Partials should not be setting any parent state. They should not know anything about the parent.

这篇关于从PartialView设置页面标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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